Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Shift() vs pop() Method in JavaScript

  • Last Updated : 18 Jan, 2021

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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
Recommended Articles
Page :