Javascript examples for Array:unshift
The unshift() method adds new items to the beginning of an array, and returns the new length.
This method changes the length of an array.
array.unshift(item1, item2, ..., itemX);
Parameter | Description |
---|---|
item1, item2, ..., itemX | Required. The item(s) to add to the beginning of the array |
A Number, representing the new length of the array
The following code shows how to Add new items to the beginning of an array:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> var fruits = ["a","b","c","d","e"]; document.getElementById("demo").innerHTML = fruits; function myFunction() {//from w w w . j av a2s . c om fruits.unshift("Lemon", "TEST"); document.getElementById("demo").innerHTML = fruits; } </script> </body> </html>