The examples below will help how one can include or remove new array elements in javascript with the help of unshift/ push/ shift/ pop functions.
unshift() is used to add one or more elements to the starting of the existing array and returns the current length of the Array.
Example:
var data = [ "1", "2" ];
var newdata = data.unshift ( "88", "99" );
alert ( data ); // displays "88,89,1,2"
alert ( newdata ); // retruns length "4" elements
push() is used to add one or more elements to the end of the existing array
Example:
var data = [ "1", "2" ];
var newdata = data.push ("88", "99" );
alert ( data ); // displays "1, 2, 88, 99"
alert ( newdata ); // retruns length "4" elements
shift() is used to remove first element from the existing array and returns the removed element.
Example:
var data = [ "1", "2", "88", "99" ];
var newdata = data.shift( );
alert ( data ); // displays "2, 88, 99"
alert ( newdata ); // returns "1"
pop() is used to remove the last element from the existing array and returns the removed element.
Example:
var data = [ "1", "2", "88", "99" ];
var newdata = data.pop( );
alert ( data ); // displays "1,2,88"
alert ( newdata ); // returns "99"
0 Comment(s)