Three JavaScript array methods to add/delete array items

There are different JavaScript array methods and Working with arrays in JavaScript is always a fun at the beginning until you hit a bug that requires a helping hand. Arrays are very useful in JavaScript and so, almost all the time, we make use of an array in our project. There are different ways to manipulate the content of an array by using array methods.

To add a specific item to an already existing array, we can use the JavaScript array method called push.

1. Using “PUSH” JavaScript array method

Push is one of the JavaScript array methods which can be used to add an item to an array and we will use the example below to show how it is done

const Students = [“Michaeal”, “John”, “Champion”, “Jim”, “Beglin”,]

assuming this this our array, and we want to add an item, we can add the item as shown below

const newStudent = “yourName”

Students.push(newStudent)

//Students.push(“yourName”)

console.log(Students);

Result
Three ways to Add/Delete JavaScript Array items

Notice that by using push, we added the item to the end of the array. What if we want add the item to the beginning of the array, then we need to use a different array method named unshift

2. Using “UNSHIFT” JavaScript array method

Unshift is one of the JavaScript array methods which can be used to add an item to an array but unlike, push, unshift insert item differently and we will use the example below to show how it is done.

Using the same array above,

const Students = [“Michaeal”, “John”, “Champion”, “Jim”, “Beglin”,]

let newStudent = “Peter”

Students.unshift(newStudent)

//Students.push(“Peter”) //you can pass in whatever you want

console.log(Students);

The new item we added to the array appears at the beginning of the array. The new item “peter” which we added, appears at he the beginning of the array.

Your next question will probably be, which of the JavaScript array methods can you use to add an item to a specific location in the array? It is simple, but first it will be important to recall that array items are stored in indexes, more specifically, array items are zero index base. The index numbering starts at 0, this means, if you have 6 items in an array, the index of the last item is 5.

Having that in mind, let us see how we can add an array to specific location or index in an array

3. Using “SPLICE” JavaScript array method

Splice is one of the JavaScript array methods which can be used to add an item to a specific location or index of an array and we will use the example below to show how it is done

const Students = [“Michaeal”, “John”, “Champion”, “Jim”, “Beglin”,]

so to insert an item into a specific location, we use the splice method, and below is an example

const Students = [“Michaeal”, “John”, “Champion”, “Jim”, “Beglin”,]

Students.splice(2,0,”joshua”)

console.log(Students);

Explanation

2—–means: Move 2 indexes. First index is “0”, second index is “1”

0—–means: Delete zero items

Joshua——means: insert “joshua” after moving 2 indexes

And our result will be

[“Michaeal”, “John”, “Champion”,”joshua”, “Jim”, “Beglin”,]

This might not be the best JavaScript array methods to add or delete array items but hopefully this gives you an idea.

5 thoughts on “Three JavaScript array methods to add/delete array items”

  1. Pingback: Two simple ways to empty an array in JavaScript - codeharis

  2. Pingback: Two simple Methods to empty the contents of an array in JavaScript - codeharis

  3. Pingback: Two simple ways to merge arrays in JavaScript - codeharis

  4. Pingback: JavaScript Spread Operator - codeharis

  5. Pingback: How to use map method in JavaScript - codeharis

Leave a Comment

Your email address will not be published. Required fields are marked *