Array methods.

Don't forget

Since we are deep diving into the JavaScript, We will forget the methods or basic concepts of the JavaScript which we learnt. So let's revise some commonly useful methods by looking into it.

1. join()

  1. The join() method joins all elements of an array into a string.
  2. It doesn't mutates the original array.

let's understand this with an example.

Example :-

let elements = ['Fire', 'Air', 'Water'];
console.log(elements.join()) //  "Fire,Air,Water"
console.log(elements.join(' ')) // "Fire Air Water"
console.log(elements.join('-')) // "Fire-Air-Water"

2. push()

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

  1. It mutates the original array and it returns the new array.

let's understand this with an example.

Example :-

let animals = ['pigs', 'goats', 'sheep'];
console.log(animals.push('cows'));  //  ["pigs", "goats", "sheep", "cows"]
console.log(animals.push('chickens')); //  ["pigs", "goats", "sheep", "cows", "chickens"]

3. flat()

1.The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. 2.It doesn't mutates the original array.

let's understand this with an example.

Example :-

let arr1 = [1, 2, [3, 4]];
arr1.flat(); // [1, 2, 3, 4]

let arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat() // // [1, 2, 3, 4, [5, 6]]

let arr3 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr3.flat(Infinity) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

4. indexOf()

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

  1. It doesn't mutates the original array.

let's understand this with an example.

Example :-

let beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison')) // output: 1
console.log(beasts.indexOf('giraffe')); //  output: -1

5. reverse()

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

  1. It mutates the original array and it returns the new array.

let's understand this with an example.

Example :-

let array1 = ['one', 'two', 'three'];
let reversed = array1.reverse();
console.log(reversed) //  ['three', 'two', 'one']

6. includes()

1.The includes() method determines whether an array includes a certain element, returning true or false as appropriate. 2.It doesn't mutates the original array.

let's understand this with an example.

Example:-

let array1 = [1, 2, 3];
console.log(array1.includes(2)); // true
console.log(array1.includes(5)) // false

7. slice()

1.The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified. 2.It doesn't mutates the original array.

let's understand this with an example.

Example:-

let animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2)); //  ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4)); //   ["camel", "duck"]
console.log(animals.slice(1, 5)); // ["bison", "camel", "duck", "elephant"]

8. forEach

1.The forEach() method executes a provided function once per array element.

  1. It doesn't mutates the original array.

let's understand this with an example.

Example:-

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

array1.forEach(function(element) {
  console.log(element);
});

// expected output: "a"
// expected output: "b"
// expected output: "c"

9. map

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

  1. It doesn't mutates the original array

let's understand this with an example.

Example:-

let array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);


console.log(map1);
// expected output: Array [2, 8, 18, 32]

10. filter

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

  1. It doesn't mutates the original array

let's understand this with an example.

Example:-

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

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

11. reduce

1.The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

  1. It doesn't mutates the original array

let's understand this with an example.

Example:-

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

let reducer = array1.reduce((accumulator, currentvalue) => {
      return accumulator + currentValue;
})

console.log(reducer) //  1 + 2 + 3 + 4  = 10;