Copyright © tutorialspoint.com

Javascript Array unshift() Method

previous next

Advertisements


Description:

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

Syntax:

array.unshift( element1, ..., elementN );

Here is the detail of parameters:

Return Value:

Returns the length of the new array. This returns undefined in IE browser.

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>

This will produce following result:

Returned array is : water,orange,mango,banana,sugar
Length of the array is : 5 

To understand it in better way you can Try it yourself.


previous next

Copyright © tutorialspoint.com