Skip to content
Related Articles

Related Articles

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

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<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> 

chevron_right


Output: 

GFG
Geeks, for, Geeks

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

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:  

GeeksforGeeks

Supported browsers:

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

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :