Array.prototype.shift()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/shift
  • Revision title: Array.shift
  • Revision id: 435323
  • Created:
  • Creator: nquinlan
  • Is current revision? No
  • Comment

Revision Content

Summary

Removes the first element from an array and returns that element. This method changes the length of the array.

Method of Array
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 3rd Edition

Syntax

array.shift()

Description

The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value.

shift is intentionally generic; this method can be called or applied to objects resembling arrays. Objects which do not contain a length property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.

Examples

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

var myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log("myFish before: " + myFish);

var shifted = myFish.shift();

console.log("myFish after: " + myFish);
console.log("Removed this element: " + shifted);

This example displays the following:

myFish before: angel,clown,mandarin,surgeon
myFish after: clown,mandarin,surgeon
Removed this element: angel

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Removes the first element from an array and returns that element. This method changes the length of the array.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.2</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>array</em>.shift()</code></pre>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>shift</code> method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value.</p>
<p><code>shift</code> is intentionally generic; this method can be <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/call" title="JavaScript/Reference/Global_Objects/Function/call">called</a> or <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply" title="JavaScript/Reference/Global_Objects/Function/apply">applied</a> to objects resembling arrays. Objects which do not contain a <code>length</code> property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Removing_an_element_from_an_array" name="Example:_Removing_an_element_from_an_array">Example: Removing an element from an array</h3>
<p>The following code displays the <code>myFish</code> array before and after removing its first element. It also displays the removed element:</p>
<pre class="brush:js">
var myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log("myFish before: " + myFish);

var shifted = myFish.shift();

console.log("myFish after: " + myFish);
console.log("Removed this element: " + shifted);
</pre>
<p>This example displays the following:</p>
<pre class="brush:js">
myFish before: angel,clown,mandarin,surgeon
myFish after: clown,mandarin,surgeon
Removed this element: angel
</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/pop" title="JavaScript/Reference/Global_Objects/Array/pop">pop</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" title="JavaScript/Reference/Global_Objects/Array/push">push</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/unshift" title="JavaScript/Reference/Global_Objects/Array/unshift">unshift</a></li>
</ul>
<!--languages({
"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/shift"
})-->
Revert to this revision