Array methods to add and remove elements

Artig
2 min readMay 2, 2020

The list of array method to add or remove elements are:

1. push(): Push add the elements at the end of the array and return the length of the new array.For example:

const array =["Reading","Cooking","Dancing"];
array.push("Swimming") // 4 (returns the length of the array)
//array = ["Reading","Cooking","Dancing",Swimming]

2. pop(): Pop removes the last element of the array and returns that element.For example:

const array =["Reading","Cooking","Dancing","Swimming","Gardening"];
array.pop() // Gardening (returns the removed element)
//array = ["Reading","Cooking","Dancing","Swimming"]

3. shift(): Shift deletes the first element of the array and returns the removed element. For example:

const array =["Reading","Cooking","Dancing","Swimming","Gardening"];
array.shift() // Reading (returns the removed element)
//array = ["Cooking","Dancing","Swimming","Gardening"]

4. unshift(): Unshift adds the elements at the beginning of the array and returns the length of the array.For example:

const array =["Cooking","Dancing","Swimming","Gardening"];
array.unshift("Reading") //5 (returns the length of new array)
//array =["Reading","Cooking","Dancing","Swimming","Gardening"]

5.splice(): Splice methods can insert, remove, and replace elements of an array. Splice mutates the original array and returns the new array.The syntax of the splice method is: array.splice(index, deleteCount, element1…elementN). For example.

const array =["Cooking","Dancing","Swimming"]
array.splice(1,0,"Reading")
//["Cooking",Reading,"Dancing","Swimming"]// returns the new array

From the above example, we can see that array. splice takes an index value, here in the example the index value was 1, and then it deletes deleteCount element , in the above example the deleteCount element was 0 which means we are not deleting any element and then inserts element1… elementN, here in the above example “Reading” i.e. (element1…elementN)was inserted.

Note:(deleteCount, element1…elementN)are optional i.e. number of items to be deleted and the number of items to be added.

6.slice(): Slice returns a new array copying to it all items from index start to end. For example:

const array = [1, 2, 3, 4]
const slicedArray = array.slice(0, 2)
// array = [1, 2, 3, 4]
// slicedArray = [1, 2]

slice method will create a new array with elements from the index range we have passed in. In the example above, we specify the start index (0) and the end index (2) which gives us a new array [1, 2]. We can see that it had not changed the old array.

Note: End index is exclusive which means that the element at that end index isn’t included in the new array.

7.concat(): The concat() method is used to join two or more arrays. For example:

const array = [1, 2, 3, 4]
const concatArray = array.concat([5, 6, 7, 8])
// array = [1, 2, 3, 4]
// concatArray = [1, 2, 3, 4, 5, 6, 7, 8]

concat method will create a new array containing all the elements of the original array. This method does not change the existing array.

Thank you for reading.

--

--