JavaScript Array shift() Method

04 Oct, 2021
Below is the example of Array shift() method.
Example:<script>function func() {           // Original array     var array = ["GFG", "Geeks", "for", "Geeks"];       // Checking for condition in array     var value = array.shift();       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.shift();       document.write(value);    document.write("<br />");     document.write(array); }   func();</script>
Output:GFG
Geeks, for, Geeks
GFG
Geeks, for, Geeks
The arr.shift() method removes the first element of the array thus reducing the size of the original array by 1.
Syntax:
arr.shift()
Parameters: This method does not accept any parameter.
Return value: This function returns the removed first element of the array. If the array is empty then this function returns undefined.
Note: This function can also be used with other javascript objects that behave like the array.
Below examples illustrate the JavaScript Array shift() method:
Example 1: In this example the shift() method removes the first element of the array, therefore it returns 34.var arr = [2, 5, 8, 1, 4];
document.write(value);
document.write(arr);
Output:34
234,567,4

var arr = [2, 5, 8, 1, 4];
document.write(value);
document.write(arr);

Output:
34
234,567,4

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 = [];
document.write(value);
document.write(arr)
Output:undefined
var arr = [];
document.write(value);
document.write(arr)

Output:
undefined
Code for the above method is provided below:
Program 1:
<script> function func() {           // Original array     var array = [34,234,567,4];       // Checking for condition in array     var value = array.shift();       document.write(value);    document.write("<br />");     document.write(array); }   func(); </script> 
Output:
34
234,567,4

Program 2:
<script>function func() {           // Original array     var array = [];       // Checking for condition in array     var value = array.shift();       document.write(value);    document.write("<br />");     document.write(array); }   func();</script>
Output:
undefined
Supported Browsers: The browsers supported by JavaScript Array shift() 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
ysachin2314
javascript-array
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?