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 a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions. I have a strong focus on API design and development, complemented by experience in web server setup and maintenance. My versatile expertise ensures seamless creation and maintenance of web applications, covering everything from intuitive user interfaces to robust server-side functionality. Passionate about coding and driven by a lifelong learning mindset, I invite you to explore more at danyal.dk.