Array.prototype.pop()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/pop
  • Revision title: Array.prototype.pop
  • Revision id: 435325
  • Created:
  • Creator: nquinlan
  • Is current revision? No
  • Comment Changed example to be more similar to Array.shift's (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift)

Revision Content

Summary

Removes the last element from an array and returns that element.

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

Syntax

array.pop()

Description

The pop method removes the last element from an array and returns that value to the caller.

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

Example

Example: Removing the last element of an array

The following code creates the myFish array containing four elements, then removes its last element.

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

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

var shifted = myFish.pop();

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

This example displays the following:

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

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.7")}} 5.5 {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Removes the last element from an array and returns that element.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/Web/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>.pop()</code></pre>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>pop</code> method removes the last element from an array and returns that value to the caller.</p>
<p><code>pop</code> is intentionally generic; this method can be <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call">called</a> or <a href="/en-US/docs/Web/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="Example" name="Example">Example</h2>
<h3 id="Example:_Removing_the_last_element_of_an_array" name="Example:_Removing_the_last_element_of_an_array">Example: Removing the last element of an array</h3>
<p>The following code creates the myFish array containing four elements, then removes its last element.</p>
<pre class="brush:js">
var myFish = ["angel", "clown", "mandarin", "surgeon"];

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

var shifted = myFish.pop();

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: angel,clown,mandarin
Removed this element: surgeon
</pre>

<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1.7")}}</td>
        <td>5.5</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push"><code>push</code></a></li>
  <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift"><code>shift</code></a></li>
  <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift"><code>unshift</code></a></li>
</ul>
Revert to this revision