JavaScript Land : Array Methods

useful array methods in JavaScript

Hii tech enthusiasts, I am Sagar and sharing my learnings along the journey in web development.

How have you been all? It was a busy week for me as some college assignments and stuff came up at the last minute.

Currently, I am learning javascript and in this article we will discuss some very common and useful array methods

1. push() :

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

basic syntax:

 const names = ['sagar', 'raj', 'ria']; //creaing an array of names 
names.push('vijay');
//'vijay' will be added to the array of names in the end of the array
// names = ['sagar', 'raj', 'ria','vijay'];

2. pop() :

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

// In the above example, names = ['sagar', 'raj', 'ria', 'vijay'];

names.pop();
//new array will be
//names = ['sagar', 'raj', 'ria'];

3. shift() :

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

names.shift();
// current array will be 
// names = ['raj', 'ria'];

4. unshift() :

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

names.unshift('paul');
//new array will be: 
//names = ['paul', 'raj', 'ria'];

5. concat():

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Guys, this is very similar to how we concatinate strings but just the syntax is different.

const names = ['paul', 'raj', 'ria']; 
const names2 = ['sagar', 'vijay'];

const array3 = names.concat(names2);
//array3 = ['paul', 'raj', 'ria', 'sagar' , 'vijay']

6. includes():

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

very self explanatory definition lol

const array3 = ['paul', 'raj', 'ria', 'sagar' , 'vijay'];
array3.includes('ria');
//will return output: true
array3.includes('roma');
//false

7. indexOf():

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.

const array3 = ['paul', 'raj', 'ria', 'sagar' , 'vijay'];
array3.indexOf('sagar');
// will return output: 3
//if there were two 'sagar' in the array, 
array3.push('sagar');
//array3 = ['paul', 'raj', 'ria', 'sagar' , 'vijay', 'sagar'];
array3.indexOf('sagar');
// will return output : 3 , only because it returns the index which comes first

8. reverse():

The reverse() method reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.

In short, just reverses the array!

const array3 = ['paul', 'raj', 'ria', 'sagar' , 'vijay'];
console.log(array3.reverse());
//OUtput: ['vijay', 'sagar', 'ria', 'raj', 'paul']

So, that's it for today guys, these are some pretty common array methods that I found useful and wanted to share with you all.

Thanks for reading till this end. You are awesome! ;)

Keep Coding...