Tuesday, November 1, 2016

Arrays Unshift() and Push()

Arrays are one of the most useful ways to store data.  A great way to add data to the beginning of an array is to use the unshift() method.

Lets take a already declared array.

var array = [1, corey, true] ;

Adding to the beginning of the array is as easy as...

array.unshift("danny", false, 15);

This adds "danny", false and 15 to the array in places [0], [1], [2] in the array and moves 1, "corey" and true to places [3], [4], [5] in the array.

If you wanted to add something to end of the array you would use the push() method..

array.push("hello", 99);

This would add "hello" and 99 to the end of the array in places [6] and [7].

Hope this helps.





No comments:

Post a Comment