How to use array unshift in JavaScript

In this article I am going to explain about unshift method in JavaScript.
  • 2267

Array unshift method in JavaScript

The array unshift method is used to inserting element in the starting position.

Syntax of unshift method

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


Parameters in unshift method:

  • element

    The element to add front of array.

Return value of  unshift  method

unshift  method returns the length of new array.

Example of unshift method

<!DOCTYPE html>

<html>

<body>

<script type="text/javascript">

    var val = new Array("one", "two", "three", "four");

 

    var length = val.unshift("First");

    document.write("Now array is : " + val);

    document.write("<br /> Length of the array is : " + length);

</script>

</body>
</html>


Output:

Clipboard06.jpg
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.