15+ Javascript Array Methods

This post is the follow-up to my previous post, the link is given below.

Learn how to use .filter() on Array in javascript

But today we will take a look at 15+ array methods of an array, let’s take a look at these methods.

.push()

This method can be used to add an element or object to the array.

Example #1: Adding a number to the array.

let arr = [1,2,3];
arr.push(4);
console.log(arr);
// output
// (4) [1, 2, 3, 4]

Example #2: Adding an object to the array.

let arr = [{id: 100, name: "Ali"}, {id: 101, name: "Shaan"}];
arr.push({id: 102, name: "Umar"});
console.log(arr);
The output of Example #2

.pop()

This method removes the last element of the array.
pop() and returns the element it removed.

Example #3:

[1, 2, 3, 4, 5].pop()
// output
// 5

const items = ["House", "Car", "Computer", "Mobile"];
items.pop();
// output
// 'Mobile'

.shift()

This method removes the first element of the array, and .shift() removes the first item of the array. It changes the original array & returns the shifted element.

Example #4:

const items = ["House", "Car", "Computer", "Mobile"];
items.shift();
// output
// 'House'

.unshift()

Add new elements to an array. it adds new elements to the beginning of an array and overwrites the original array.

Example #5:

const items = ["House", "Car", "Computer", "Mobile"];
items.unshift("Table", "Door");
// output
// 6
// modified items
// ['Table', 'Door', 'House', 'Car', 'Computer', 'Mobile']

Array methods card

Hope you like the post, remember to share this with others.

See you in the next post.

Author: Danyal
I'm working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.