JavaScript Array unshift() Method - GeeksforGeeks

04 Oct, 2021
Below is the example of Array unshift() method.
Example:<script>function func() {           // Original array     var array = ["GFG", "Geeks", "for", "Geeks"];       // Checking for condition in array     var value = array.unshift("GeeksforGeeks");       document.write(value);    document.write("<br />");     document.write(array); }   func();</script>
<script>function func() {           // Original array     var array = ["GFG", "Geeks", "for", "Geeks"];       // Checking for condition in array     var value = array.unshift("GeeksforGeeks");       document.write(value);    document.write("<br />");     document.write(array); }   func();</script>
Output:5
GeeksforGeeks,GFG,Geeks,for,Geeks
5
GeeksforGeeks,GFG,Geeks,for,Geeks
The arr.unshift() method is used to add one or more elements to the beginning of the given array. This function increases the length of the existing array by the number of elements added to the array.
Syntax:
array.unshift(element1, element2, ..., elementX)

Parameters: This method accept a single parameters.
element: This parameter elements to be added at the beginning of the array.
Return value: This function returns the new length of the array after inserting the arguments at the beginning of the array.
Below examples illustrate the JavaScript Array unshift() method:
Example 1: In this example the function unshift() adds 28 and 65 to the front of the array.var arr = [23,76,19,94];
document.write(arr.unshift(28,65));
document.write(arr);
Output:6
28,65,23,76,19,94

var arr = [23,76,19,94];
document.write(arr.unshift(28,65));
document.write(arr);

Output:
6
28,65,23,76,19,94

Example 2: In this example the shift() method tries to remove the first element of the array, but the array is empty, therefore it returns undefined.var arr = [23,76,19,94];
document.write(arr.unshift());
document.write(arr);
Output:4
23,76,19,94
var arr = [23,76,19,94];
document.write(arr.unshift());
document.write(arr);

Output:
4
23,76,19,94
Code for the above method is provided below:Program 1:
// JavaScript to illustrate unshift() function<script>function sayHello() {    var arr = [23,76,19,94];           // Adding elements to the front of the array    document.write(arr.unshift(28,65));    document.write("<br>");    document.write(arr);}sayHello();</script>
Output:
6
28,65,23,76,19,94

Program 2:
// JavaScript to illustrate unshift() function<script>function sayHello() {    var arr = [23,76,19,94];           // Adding elements to the front of the array    document.write(arr.unshift(28,65));    document.write("<br>");    document.write(arr);}sayHello();</script>
Output:
4
23,76,19,94
Supported Browsers: The browsers supported by JavaScript Array unshift() method are listed below:
Google Chrome 1 and above
Edge 12 and above
Firefox 1 and above
Internet Explorer 5.5 and above
Opera 4 and above
Safari 1 and above
sakthipreetha2001
ysachin2314
javascript-array
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?