Basic Array Methods in JavaScript.

Basic Array Methods in JavaScript.

Methods used to insert/add elements in an array

In an array, we can add or insert elements using square brackets but few methods can handle this operation.

  • push() method

It is the method that is used to append the elements at the end of an array. The element that should be appended to the array will be given as an argument to the push() method.

let arr = [10,20,30,'Jack','Jim'];
arr.push('King');
console.log(arr); 
//Output : [10,20,30,'Jack','Jim','King']//

As we can see 'King' got appended at the end of an array.

  • unshift() method

If we need to insert the elements from the beginning of an array then unshift() method can be handy for us.

The operation of this method is to add the elements at the beginning of an array. As we have seen in the push() method, we pass the elements that should be added to the array as the arguments.

arr.unshift(1);
console.log(arr);
//Output : [1,10,20,30,'Jack','Jim','King']//

1 is inserted at the beginning of the array.

We can add multiple elements using push() and unshift() methods and both returns the length of an array

let arr2=['Virat','Rohit'];
arr2.unshift('Sachin','Sehwag');
arr2.push('Gill','Gaikwad');
console.log(arr2);
//Output : ['Sachin','Sehwag','Virat','Rohit','Gill','Gaikwad']//

We can observe that Sachin and Sehwag both names are added at the beginning of an array by unshift() method and Gill and Gaikwad are appended at the end of an array.

Methods used to remove an element from an array

  • pop() method

The pop() method is used to remove the last element of an array and It returns the element which has been removed.

let arr3=[2,4,6,8,10];
const removed1=arr3.pop();     // 10 is removed and stored in the removed1 variable
const removed2=arr3.pop();    // 8 is removed and stored in removed2 variable
console.log(removed1,removed2,arr3);

//Output : 10,8,[2,4,6]
  • shift() method

The shift() method is used to remove the elements that are at the beginning of an array. Same as the pop() method it will also return the removed element.

let arr4=['India','Sri Lanka','Italy','France'];
arr4.shift(); // First element 'India' will be removed and now Sri Lanka will be first element
const secondRemoved=arr4.shift();       // Now Sri Lanka will be removed     
console.log(arr4);
console.log(secondRemoved);
/*
Output : ['Italy','France'] 
               Sri Lanka
*/

push() and pop() methods are faster than the shift() and unshift() methods.

Methods used to search and check an element in the array

  • at() method

It is used to search for an element in an array based on its index value. It has been introduced in ES6.

This method takes the index value as the argument and returns the element if the element is present in the given index position or else it returns undefined.

const arr5 =['JavaScript','Python','Java','C++'];
let elementAt2 = array.at(2); 
console.log(elementAt2);
// Output : Java

The element present in index 2 is Java.

- indexOf() method

It is the method used to find the index of a specific element. The element itself is given as an argument for the method.

It returns the index of the element if it exists or else it will return -1.

Consider the same array arr5

console.log(arr5.indexOf('JavaScript'),arr5.indexOf('Physics'));
//Output : 0,-1

The index of the element 'JavaScript' is 0 but there is no element 'Physics'. So it has returned -1.

  • includes() method

It is the method that is used to check whether the element is present in the array. If the element is present in the array then it returns true or else it returns false.

Consider the array arr5

console.log(arr5.includes('Python'))   //Output : true
console.log(arr5.includes('ReactJs'))  //Output : false

Python is present in the array so it returned true but ReactJs is not present in the array so it returned false.

So these are the basic array methods in Javascript

Summary

  • push() method adds the element at the end of an array.

  • unshift() method adds the element in the beginning of an array.

  • pop() method removes the element from the end of an array.

  • shift() method removes the element from the beginning of an array.

  • at() method finds the element using an index value.

  • indexOf() method finds the index of the element using the element itself.

  • includes() method checks whether the element is present in the array or not.