Tuesday, June 12, 2018

JavaScript - Array Methods - push , pop, shift , unshift

Below code demonstrates the usage of the following array methods:

  • push()
  • pop()
  • shift()
  • unshift()



var Arr1 = [1,2,3,4,5,,];

console.log(Arr1);


// adding element 6 at the end of array
var a = 6;
Arr1.push(a);
console.log("Added Element at End : "+a);
console.log(Arr1);

// removing element from the end of array
var b = Arr1.pop();
console.log("Removed Element from End : "+b);
console.log(Arr1);

//removing element from start of array
var c = Arr1.shift();
console.log("Removed Element from Start : "+c);
console.log(Arr1);

// adding element to start of array
var d = 1;
Arr1.unshift(d);
console.log("Added Element to Start : "+d);
console.log(Arr1);

Try it at : http://jsbin.com

Refer:
push - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
pop - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
shift - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
unshift - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift




No comments:

Post a Comment