Tuesday, November 8, 2011

Javascript: Array functions Push, Unshift, Pop, Shift

Push : The push method adds an element to the end of the array. If the method takes more than one elements, they are appended from left to right order.

var arraydata = ['a','b'] ;
arraydata.push('c');
arraydata.push('d','e');
console.log(arraydata);

Output
['a','b','c','d','e']


Unshift :The unshift method adds an element to the start of the array. If the method takes more than one elements, they are appended from right to left order.

var arraydata = ['b','a'] ;
arraydata.unshift('c');
arraydata.unshift('e','d');
console.log(arraydata);

Output
["e", "d", "c", "b", "a"]

Pop: The pop method removes an element from the end of the array and returns it.

var arraydata= ['a','b','c','d','e'];
console.log(arraydata.pop());
console.log(arraydata);

Output:
e
["a", "b", "c", "d"]

Shift : The shift method removes an element from the start of the array and returns it.

var arraydata= ['a','b','c','d','e'];
console.log(arraydata.shift());
console.log(arraydata);

Output:
a
["b", "c", "d", "e"]

No comments: