Javascript Array element remove

Description

Javascript Array element remove

 P:Removing an element from the end of an array is using the pop() function: 
let nums = [1,2,3,4,5,9]; 
nums.pop(); /*from   w w  w.  java2s . c  om*/
console.log(nums); // 1,2,3,4,5 

The function to remove an element from the beginning of an array is shift().

Here is how the function works:

let nums = [9,1,2,3,4,5]; 
nums.shift(); /*from   ww  w.j  a  v a2 s. co m*/
console.log(nums); // 1,2,3,4,5 

Both pop() and shift() return the values they remove, so you can collect the values in a variable:

let nums = [6,1,2,3,4,5]; 
let first = nums.shift(); // first gets the value 9 
nums.push(first); //  w  w  w .j a v a2  s .  c om
console.log(nums); // 1,2,3,4,5,6 



PreviousNext

Related