Javascript - Add and delete objects from begining and end of an Arrays with examples


Arrays Javascript 

 The array is used to store a collection of objects in insertion ordered Array Supports Queue and stack operations, It has methods which we can add an element to start/end of an array. delete an element from start and end of an array

Push() method example

 This method is used to add the element to the end of the array Syntax

push(item0 : Object, [item1 : Object, [...]]) : Number  

Here is an example push() method usage

let strs = ["one", "two"];  
strs.push("three");  
console.log( strs ); // one, two, three  

pop() method example 

This removes an element from the end of Array
Here is an example pop() method usage

let strs = ["one", "two","three"];  
strs.pop();  
console.log( strs ); // one, two  

shift() method example 

shift() method removes the elements from the start of the array and returns the first element from the array string
Here is an example shift() method usage

let strs = ["one", "two","three"];  
console.log( strs.shift() ); // one  
console.log( strs ); // two three  

unshift() method example

unshift() method add the object to start of the array and returns the size of the newly modified array
Here is an example unshift() method usage

let strs = ["one", "two","three"];  
console.log( strs.unshift('start') ); // 4  
console.log( strs ); // start one two three  

Similar Posts