Javascript array unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
Its syntax is as follows −
array.unshift( element1, ..., elementN );
element1, ..., elementN − The elements to add to the front of the array.
Returns the length of the new array. It returns undefined in IE browser.
Try the following example.
<html> <head> <title>JavaScript Array unshift Method</title> </head> <body> <script type="text/javascript"> var arr = new Array("orange", "mango", "banana", "sugar"); var length = arr.unshift("water"); document.write("Returned array is : " + arr ); document.write("<br /> Length of the array is : " + length ); </script> </body> </html>
Returned array is : water,orange,mango,banana,sugar Length of the array is : 5