Javascript Array Methods (16 Methods That Every Developer Must Know )

In the previous article, we learn about Javascript arrays. In this article, we are going to learn Javascript Array Methods.

What are Javascript Array Methods?

Javascript Array Methods are useful while performing operations on arrays. In this post, I will explain 16 of most important Javascript Array Methods. There are few more Array methods are. But these 16 are the most used and plays an important role in array operations.

So, let’s get started.

1.length

Length method returns the length of an array.

Syntax :  array_name.length;

Example : 

var arr = [1,2,3,4,5];
console.log(arr.length);
//prints 5

Note: here the length means, the number of elements.

2.isArray

This method identifies whether the passed value is an array or not. Displays true if it is an array and displays false if it is not an array.

Syntax : Array.isArray(array_name);

Example : 

var arr = [ ];
console.log(Array.isArray(arr));
//returns true.

3.concat

This method clubs or merges two or more arrays into one.

Syntax :  arr1.concat(arr2);

Example :

var arr1 = [1,2];
var arr2 = [3,4];
console.log(arr1.concat(arr2));
//prints [1,2,3,4]

4.push

The push method inserts an element into an array from the right side.

Syntax : array_name.push(element);

Example :

var arr = [1,2];
arr.push(3);
console.log(arr);
//prints [1,2,3]

5.pop

The pop method removes an element from the array from the right side.

Syntax: array_name.pop();

Example :

var arr = [1,2,3];
arr.pop();
console.log(arr);
//prints [1,2]

6.join

The join method clubs all the elements of an array into a single string.

Syntax : array_name.join(seperator);

Here the separator is to be added after each element of an array. If you don’t specify a separator the default comma will be placed after each element.

Example : 

var arr = [1,2,3,4];
console.log(arr.join());
//prints "1,2,3,4"
console.log(arr.join(""));
//prints "1234"

7.reverse

The reverse method changes the positions of array elements into reverse chronological order.

Syntax : array_name.reverse();

Example :

var arr = [1,2,3,4,5];
console.log(arr.reverse());
//prints [5,4,3,2,1];

8.shift

The shift method deletes and returns the first element of an array.

Syntax : array_name.shift();

Example :

var arr = [1,2,3,4];
console.log(arr.shift());
// prints 1
console.log(arr)
//prints [2,3,4]

9.slice

The slice method cuts the array from the starting index to the ending index. But the actual array doesn’t affect.

Syntax : array_name.slice(start_index,end_index);

Example :

var arr = [1,2,3,4,5];
console.log(arr.slice(1,3));
//prints [2,3]
console.log(arr);
//prints [1,2,3,4,5]

10.sort

The sort method sorts all the array elements into chronological order.

Syntax :  array_name.sort();

Example : 

var arr = [5,4,3,2,1];
console.log(arr.sort());
//prints [1,2,3,4,5];

11.unshift

The unshift method adds the elements to the array at the beginning.

Syntax : array_name.unshift(elm1,elm2,elm3);

Example :

var arr = [4,5];
arr.unshift(1,2,3);
console.log(arr);
//prints [1,2,3,4,5]

12.splice

The splice method adds or removes an array element.

Syntax: array_name.splice(start_index,delete_count,item1,item2...itemN);

start_index – where to start

delete_count – how many elements to be deleted(optional).

Example:

var arr = [1,2,3,4,5];
arr.splice(1,0,"js"); // adds "js" after the element 1
console.log(arr);
//output  [1, "js", 2, 3, 4, 5]
arr.splice(4,1); // removes element 4
console.log(arr);
//output [1, "js", 2, 3, 5]

13.fill

The fill method fills an array with a given value from start to end.

Syntax : array_name.fill(value,start_index,end_index);

Example :

var arr = [1,2,3,4,5];
console.log(arr.fill(0,1,3));
//prints [1, 0, 0, 4, 5]

14.includes

The includes method checks whether the array does have the given value or not. Returns true, if have and returns false if it doesn’t have.

Syntax : array_name.includes(value);

Example :

var arr = [1,2,3,4,5];
console.log(arr.includes(2));
//prints true

15.indexOf

The indexOf method finds the index of a given element.

Syntax : array_name.indexOf(element);

Example :

var arr = [1,2,3,4];
console.log(arr.indexOf(2));
//prints 1

16.lastIndexOf

The lastIndexOf method exactly works like an indexOf method except it counts the index from the right side.

Syntax: array_name.lastIndexOf(element);

Example :

var arr = [1,3,4,3,5];
console.log(arr.lastIndexOf(3));
//prints 3

The methods which I listed above are the most used methods.

But there are other plenty of javascript array methods, but we use them rarely. If you have interest, here is the link for you.

Hoping! this article may help you. If you have any doubts feel free to comment below.

If you like this article please share with your friends.

 

Thanks for reading!

Happy Coding!

Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Yes0
No0
Powered by Pixelbart

Leave a Reply

Your email address will not be published. Required fields are marked *