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 skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk