Most important JavaScript array methods.

Md Tajuuddin Tarek
5 min readJul 12, 2021

In this blog, I gonna show you the most Important 20 Array methods. As a JavaScript developer, These methods, you must know. So, let’s start…

Method 1: Array.isArray()

This method is used to verify whether a variable is an array.

Example:

const Array1 = [1, 2, 3];console.log(Array.isArray(Array1))// output is: trueconst Array2 = { id: 1, name:”taju” }console.log(Array.isArray(Array2))// output is: false

Method 2: Array.of()

This method is used to convert any variable into an array.

Example:

const array1 = 1;const array2 = 2;console.log(Array.of(array1, array2));// output is: [1, 2]const object1 = { id:1, name:”taju” }const object2 = { id:2, name:”korim” }console.log(Array.of(object1, object2));// output is: [ {id:1, name:”name”}, {id:2, name://    ”korim”} ]

Method 3: concat()

The full form of concat is concretition. This means that this method is used to convert multiple arrays or variables into one array.

Example:

const array1 = [‘a’, ‘b’, ‘c’];const array2 = [‘d’, ‘e’, ‘f’];const array3 = array1.concat(array2)console.log(array3)// output is: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];

Method 4: filter()

Just as we filter something to clean the dirt, this method is used to remove the unnecessary things from the array and sift the necessary things.

Example:

const array = [{id:1, profession:”student”, name:”Taju”},{id:2, profession:”student”, name:”Korim”},{id:3, profession:”businessman”, name:”Rasel”}];const ArrayFiltered = array.filter(element => element.profession === “student”)console.log(ArrayFiltered)// output is: [{id:1, profession:”student”, name:”Taju”}, {id:2, // profession:”student”, name:”Korim”}]

Method 5: find()

Find means to search. This method is used to find a specific element in an array.

Example:

const array = [12, 13, 14, 15]const ArrayFind = array.find(element => element ===15)console.log(ArrayFind)// output is: [ 15 ]

Method 6: findIndex()

The position of each element of the array is called Index. For example: [“Taju”, “Sakil”, “Tripti”, “Ajom”] Here “Taju” Index is 0, because the index of the array starts from 0 accordingly.

Taju = 0;

Sakil = 1;

Tripti = 2;

Ajom = 3;

In a word, this method is used to find the index of an element.

Example:

const array = [“Taju”, “Sakil”, “Tripti”, “Ajom”]const index = array.findIndex(name => name === “Ajom”)console.log(index)// output is: 3

Method 7: flat()

Flat means equal or flat. This method is used to convert multiple arrays to one or more arrays.

Example:

const arr1 = [0, 1, 2, [3, 4]];console.log(arr1.flat());// expected output: [0, 1, 2, 3, 4]const arr2 = [0, 1, 2, [[[3, 4]]]];console.log(arr2.flat(2));// expected output: [0, 1, 2, [3, 4]]

Method 8: map()

This method is called array life. It’s a lot like For-Loop, its job is to read every element of the array.

Example:

const array1 = [1, 4, 9, 16];const map1 = array1.map(x => x * 2);console.log(map1);// expected output: Array [2, 8, 18, 32]

Method 9: flatMap()

There is nothing new to say about flatMap(). flatMap() is a combination of flat() and map().

Example:

let arr1 = [1, 2, 3, 4];arr1.map(x => [x * 2]);// [[2], [4], [6], [8]]arr1.flatMap(x => [x * 2]);// [2, 4, 6, 8]// only one level is flattenedarr1.flatMap(x => [[x * 2]]);// [[2], [4], [6], [8]]

Method 10: includes()

This method is used to check if there are any elements in the array.

Example:

const array1 = [1, 2, 3];console.log(array1.includes(2));// expected output: trueconst pets = ['cat', 'dog', 'bat'];console.log(pets.includes('cat'));// expected output: trueconsole.log(pets.includes('at'));// expected output: false

Method 11: indexOf()

indexOf() is a lot like findIndex(). It is used to find the index of an element. If there is no element then its index will be -1.

Example:

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];console.log(beasts.indexOf('bison'));// expected output: 1// start from index 2console.log(beasts.indexOf('bison', 2));// expected output: 4console.log(beasts.indexOf('giraffe'));// expected output: -1

Method 12: lastIndexOf()

lastIndexOf() works exactly like indexOf(), but in reverse.

Example:

const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];console.log(animals.lastIndexOf('Dodo'));// expected output: 3console.log(animals.lastIndexOf('Tiger'));// expected output: 1

Method 13: join()

Join means to add. The method of adding all the elements of an array is join(). To put something in the middle, you have to write with a string (‘’).

Example:

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"

Method 14: sort()

This method is used for sorting. This means that up to A-Z is serially sorted, in which case the number and the capital letter sit first. Small letter and boolean type sit at the end of all.

Example:

const array = ["Didar", "Adom", "Babor", "chandni",12, true];console.log(array.sort());// Output is: [12, "Adom", "Babor", "Didar", "chandni", true]

Method 15: reverse()

The function of this method is to reverse all the elements of the array.

Example:

const array = ["Didar", "Adom", "Babor", "chandni",12, true];console.log(array.reverse());// Output is: [true, 12, "chandni", "Babor", "Adom", "Didar"]

Method 16: pop()

The function of this method is to assign the last element of the array.

Example:

const array = [11, 12, 13, 14, 15]array.pop()console.log(array)// Output is: [11, 12, 13, 14]

Method 17: push()

The function of this method is to add one or more new elements to the end of the array.

Example:

const arr = [11, 12, 13, 14]arr.push(15)console.log(arr)// Output is: [11, 12, 13, 14, 15]const arr2 = [11, 12, 13]arr2.push(14, 15)console.log(arr2)// Output is: [11, 12, 13, 14, 15]

Method 18: slice()

Slice means to cut into pieces. That means cutting an array into smaller ones is its job. It has to give one or a maximum of two parameters. The parameters must be index numbers. If an index number is given as a parameter, it will cut up to that index number and return the rest of the element. And if you give two parameters, he will take the element from the first parameter to the second parameter and subtract the rest.

Example:

const arr = [10, 11, 12, 13, 14]console.log(arr.slice(2))// Output is: [12, 13, 14]const arr2 = [10, 11, 12, 13, 14, 15]console.log(arr2.slice(2, 4))// Output is: [12, 13]

Method 19: splice()

Splice means to split. It replaces and slices any element of the array. It has to give three parameters. The first is the index number, the second is the slice number and the third is the key to replace.

Example:

const months = ['Jan', 'March', 'April', 'June'];months.splice(1, 0, 'Feb');// inserts at index 1console.log(months);// expected output: Array ["Jan", "Feb", "March", "April", "June"]months.splice(4, 1, 'May');// replaces 1 element at index 4console.log(months);// expected output: Array ["Jan", "Feb", "March", "April", "May"]

Method 20: shift() and unshift()

The shift method is a lot like pop(). But it works the other way around, it removes an element from the first of the array. unshift() is a lot like push() method. But it works just the opposite. It adds one or more new elements to the beginning of the array.

Example:

const array1 = [1, 2, 3];const firstElement = array1.shift();console.log(array1);// expected output: Array [2, 3]console.log(firstElement);// expected output: 1const array1 = [1, 2, 3];console.log(array1.unshift(4, 5));// expected output: 5

--

--