Skip to content
Related Articles

Related Articles

Shift() vs pop() Method in JavaScript

Improve Article
Save Article
  • Last Updated : 18 Jan, 2021
Improve Article
Save Article

JavaScript shift() and pop() methods are used to remove an element from an array. But there is a little difference between them. Shift() method removes the first element and whereas the pop() method removes the last element from an array. 

  • The Shift() returns the removed first element of the array. If the array is empty then this function returns undefined whereas the pop() method turns the removed element array. If the array is empty, then this function returns undefined.
  • Both the method is used for reducing the length of the array by 1.
  • Both shift() and pop() are the in-built methods of the object array.
  • Both methods will change the original array.

Example: Below is an example of the array shift() method

Javascript




<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

Example 2: Below is the example of the array pop() method.

Javascript




<script> 
    function func() { 
        var arr = ['GFG', 'gfg', 'g4g', 'GeeksforGeeks']; 
      
        // Popping the last element from the array 
        document.write(arr.pop());
    
    func(); 
</script>                 

Output:  

GeeksforGeeks

Supported browsers:

  • Google Chrome
  • Microsoft Edge
  • Mozilla Firefox
  • Safari
  • Opera

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!