• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • Array.prototype.shift()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)
      • 正體中文 (繁體)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. Array.from()
    2. Array[Symbol.species]
  • Instance methods
    1. Array.prototype.entries()
    2. Array.prototype.find()
    3. Array.prototype.flat()
    4. Array.prototype.indexOf()
    5. Array.prototype.map()
    6. Array.prototype.reduceRight()
    7. Array.prototype.some()
    8. Array.prototype.toReversed()
    9. Array.prototype.unshift()
    10. Array: length
    11. Function.prototype.bind()
    12. Function: displayName Non-standard
    13. Function.prototype.arguments Non-standard Deprecated
    14. Function.prototype.caller Non-standard Deprecated
  • Instance methods
    1. Object.prototype.__defineGetter__() Deprecated
    2. Object.prototype.__defineSetter__() Deprecated
    3. Object.prototype.__lookupGetter__() Deprecated
    4. Object.prototype.__lookupSetter__() Deprecated
    5. Object.prototype.toLocaleString()
    6. Object.prototype.__proto__ Deprecated
    7. Array instances removes the first element from an array and returns that removed element. This method changes the length of the array.

  • Try it

    const array1 = [1, 2, 3];
    
    const firstElement = array1.shift();
    
    console.log(array1);
    / Expected output: Array [2, 3]
    
    console.log(firstElement);
    / Expected output: 1
    

    Syntax

    js
    shift()
    

    Parameters

    None.

    Return value

    The removed element from the array; undefined is returned.

    The pop() method has similar behavior to shift(), but applied to the last element in an array.

    The shift() method is a arr.slice(1) instead.

    The shift() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

    Examples

    Removing an element from an array

    The following code displays the myFish array before and after removing its first element. It also displays the removed element:

    js
    const myFish = ["angel", "clown", "mandarin", "surgeon"];
    
    console.log("myFish before:", myFish);
    / myFish before: ['angel', 'clown', 'mandarin', 'surgeon']
    
    const shifted = myFish.shift();
    
    console.log("myFish after:", myFish);
    / myFish after: ['clown', 'mandarin', 'surgeon']
    
    console.log("Removed this element:", shifted);
    / Removed this element: angel
    

    Using shift() method in while loop

    The shift() method is often used in condition inside while loop. In the following example every iteration will remove the next element from an array, until it is empty:

    js
    const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"];
    
    while (typeof (i = names.shift()) !== "undefined") {
      console.log(i);
    }
    / Andrew, Tyrone, Paul, Maria, Gayatri
    

    Calling shift() on non-array objects

    The shift() method reads the length property of this. If the deleted, and the length property is decremented by one.

    js
    const arrayLike = {
      length: 3,
      unrelated: "foo",
      2: 4,
    };
    console.log(Array.prototype.shift.call(arrayLike));
    / undefined, because it is an empty slot
    console.log(arrayLike);
    / { '1': 4, length: 2, unrelated: 'foo' }
    
    const plainObj = {};
    / There's no length property, so the length is 0
    Array.prototype.shift.call(plainObj);
    console.log(plainObj);
    / { length: 0 }
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-array.prototype.shift

    Browser compatibility

    See also

    • Indexed collections guide
    • Array
    • Array.prototype.push()
    • Array.prototype.pop()
    • Array.prototype.unshift()
    • Array.prototype.concat()
    • Array.prototype.splice()

    Help improve MDN

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant