The Wayback Machine - https://web.archive.org/web/20220305154426/https://www.geeksforgeeks.org/javascript-array-shift-method/amp/

JavaScript Array shift() Method

Below is the example of Array shift() method.

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:

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:




Article Tags :