Javascript Array methods you need to know!

Javascript Array methods you need to know!

Nure Kamal Mithu's photo
Nure Kamal Mithu
·Nov 2, 2020·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Javascript Array methods you need to know!

Image for post

What can you call an Array in Javascript? special variable or list-like objects?

Well, both of these define Array in Javascript. It is a primary notion that an array can hold more than one value at a time. To store data, manipulate data, or some other data-driven interaction, Array can be a great choice to work with.

With a view to making these interactions smoother, effective, and specific, Javascript provides some methods to apply with Array and get benefit from it. Let’s have a look!

  1. map()

Get hassled while using for loop in an Array? Well, map() method can be a better solution for you. It takes a parameter that indicates each Array elements and then returns you a new array after doing something.


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

// pass a function to map

const map = => x +1);


// expected output: Array [2, 3, 4, 5]

2. concat()

Need two array elements in a new array? Then concat() method will help you. Just take the last array name as concat parameter that you want to add after the first array, and there you go.

Example :

var arr1 = [1,2,3]; var arr2 = [4,5,6]; var arr3 = arr1.concat(arr2);


// expected output: [1,2, 3, 4, 5,6]]

3. pop()

pop() is used to remove the last element of an array. In return, it gives you the value that removed.


const fruits = [‘mango’, ‘orange’, ‘banana’];


// expected output: “banana”

4. filter()

Yes, you got it right. This semantic method gives you the filtered value from an Array. Just takes an argument of each element and a function that provides the condition to be filtered. And of course, it will give you a filtered Array.


var prices = [15, 45, 1234, 65, 122, 555];

var result = prices.filter(price => price < 100);


// expected output: [1234, 122, 555]

5. find()

You will need to find() method very handy to find a specific element in an array. Unlike filter, it will return the first element only.


var prices = [15, 45, 1234, 65, 122, 555];

var result = prices.find(price => price < 100);


// expected output: 1234

6. reduce()

reduce() is an interesting and a bit confusing method also. It takes 4 arguments- Accumulator (acc), Current Value (cur), Current Index (idx) Source Array (src). With a callback function, it takes actions with the current value and accumulator and returns one value at the end.


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

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

// 1 + 2 + 3 + 4


// expected output: 10

// 5 + 1 + 2 + 3 + 4

console.log(array1.reduce(reducer, 5));

// expected output: 15

7. join()

join() method helps you to concatenate in an Array. It takes the separated value as a parameter or takes the default comma parameter while joining together all array elements. It gives you a string at the end.


const elements = [‘Fire’, ‘Air’, ‘Water’];


// expected output: “Fire,Air,Water”

8. sort()

To sort all array elements sort() method can be applied. It sorts primarily as ascending order, it returns a new array with sorted elements.


const array1 = [1, 30, 4, 21, 100000];



// expected output: Array [1, 100000, 21, 30, 4]

9. shift()

Want to remove the first element of an array? Then shift() method is for you. It returns the shifted element and changes the array length.


const array1 = [1, 2, 3];

const firstElement = array1.shift();


// expected output: Array [2, 3]

10. lastIndexOf()

lastIndexOf() method returns the last index of a common element in an Array.


const animals = [‘Dodo’, ‘Tiger’, ‘Penguin’, ‘Dodo’];


// expected output: 3

That’s all for this article so far. Apply these methods for your next web projects. And of course, treat me with some claps!

Share this