JavaScript - POP, PUSH,SHIFT and UNSHIFT
POP : The pop() method removes the last element from an array and returns it.
var numbers = [0,1,2,3,4]; //remove last number console.log(numbers.pop()); //4 //new array console.log(numbers); //output - [0,1,2,3]PUSH : The push() method adds a new elemnet at the end of an array and returns the new array length.
var numbers = [0,1,2,3,4]; //add number at the end //6 is the length of the new array console.log(numbers.push(5)); //6 //new array console.log(numbers); //output - [0,1,2,3,4,5]SHIFT : The shift() method removes the first array element of an array and returns it.
var numbers = [0,1,2,3,4]; //remove number at the beginning //returns removed number console.log(numbers.shift()); //0 //new array console.log(numbers); //output - [1,2,3,4]UNSHIFT : The unshift() method adds a new element to at the beginning of an array and returns the new array length.
var numbers = [0,1,2,3,4]; //add the number at the beginning //6 is the length of the new array console.log(numbers.unshift(5)); //6 //new array console.log(numbers); //output - [5,0,1,2,3,4]
No comments: