Using JavaScript unshift() method you can add one or more elements to the beginning of the given array. This methods change the length of array by increases the length of the existing array by the number of elements added to the array.
Syntax
array.unshift(item1, item2, ..., itemX)
Parameter Values
items: Elements to be added at the beginning of the array.
Example of JavaScript unshift Method
Let’s see how to Add new items to the beginning of an array in JS.
<!DOCTYPE html>
<html>
<body>
<script>
var arr1 = ["A", "B", "C", "D"];
arr1.unshift("X", "Y");
alert(arr1)
</script>
</body>
</html>
Output:
Do comment if you have any doubts, suggestions and questions in this tutorial.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version