Javascript Inbuilt methods: Part III

Array Data Types: In this chapter we’ll learn about the inbuilt methods for array data structure.

concat(): This method merges two or more arrays.

For example Here are two arrays:

const array1 = ['yellow', 'orange', 'red'];
const array2 = ['blue', 'green', 'pink'];

Now we need to combine both array in one array, we can use concat method.

const result = array1.concat(array2);   


Expected output: Array ['yellow', 'orange', 'red', 'blue', 'green', 'pink']

filter(): The method creates a new array with all elements that pass the test implemented by the provided function.

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; 

const result = words.filter(test(words)); 

function test(words) {   

  const result = [];   

  for (let I = 0; I <words.length; i++) {     
   if (words[i].length > 6) {      

  return result;


// expected output: Array ["exuberant", "destruction", "present"]

find(): The method returns the value of the first element in the provided array that satisfies the test by the provided function.

For Example:

const array1 = [5, 12, 8, 130, 44]; 

const found = array1.find(element => element > 10); 

console.log(found); // expected output: 12

findIndex(): The method returns the index of the first element in the array that satisfies the test by provided function. Otherwise, it returns -1, indicating that no element passed the test.

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;


// expected output: 3

forEach(): The forEach() method executes a provided function once for each array element.

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));
 // expected output: "a"
 // expected output: "b"
 // expected output: "c" 

includes(): This method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

const array1 = [1, 2, 3];


// expected output: true

const pets = ['cat', 'dog', 'bat'];


// expected output: true


// expected output: false

indexOf(): The method returns the first index at which a given element can be found in the array, or -1 if it is not present.

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];


// expected output: 1

join(): The method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

const elements = ['Fire', 'Air', 'Water']; console.log(elements.join());  // expected output: "Fire,Air,Water" console.log(elements.join(''));  // expected output: "FireAirWater" console.log(elements.join('-'));  // expected output: "Fire-Air-Water"

map(): The method creates a new array populated with the results of calling a provided function on every element in the calling array.

const array1 = [1, 4, 9, 16];
// pass a function to map
 const map1 = => x * 2);
 // expected output: Array [2, 8, 18, 32]

pop(): The method removes the last element from an array and returns that element. This method changes the length of the array.

const colors = ['red', 'orange', 'yellow', 'blue', 'green'];


// output: "green"


// output: Array ['red', 'orange', 'yellow', 'blue']

push() method adds one or more elements to the end of an array and returns the new length of the array.

const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');

slice():  The method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

// expected output: Array ["camel", "duck", "elephant"]

reverse(): The method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

const array1 = ['one', 'two', 'three'];

console.log('array1:', array1);

reduce(): The method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

const array1 = [1, 2, 3, 4];

const reducer = (accumulator, currentValue) => accumulator + currentValue;

shift():  The method is often used in condition inside while loop. In the following example every iteration will remove the next element from an array, until it is empty.

var myFish = ['angel', 'clown', 'mandarin', 'surgeon']; 
var shifted = myFish.shift(); 

console.log('myFish after:', myFish); 
// myFish after: ['clown', 'mandarin', 'surgeon']

unshift() The method adds one or more elements to the beginning of an array and returns the new length of the array.

const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));

// expected output: 5

keys() The method returns a new Array Iterator object that contains the keys for each index in the array.

const array1 = ['a', 'b', 'c']; 
const iterator = array1.keys(); 
for (const key of iterator) {    
// expected output: 0  
// expected output: 1  
// expected output: 2