JavaScript » Array » unshift

Syntax:
Array.unshift(element1,..., elementN)

The unshift method adds one or more elements to the beginning of an array and returns the new length.

Examples

Code:
trees = ["beech", "maple", "sycamore"]
document.write(trees.unshift("oak", "ash"))
document.write("<BR>" + trees)
Output:
5
oak,ash,beech,maple,sycamore
Explanation:

The following code first creates an array called 'trees' and then uses the unshift method to add two new elements to the beginning of it, returning the new length of the array. Finally, the third line of code displays all the elements of the altered array.