mozilla

Revision 26330 of Array.prototype.unshift()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/unshift
  • Revision title: Array unshift method
  • Revision id: 26330
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment Core JavaScript 1.5 Reference:Objects:Array:unshift moved to Core JavaScript 1.5 Reference:Global Objects:Array:unshift

Revision Content

Summary

Adds one or more elements to the beginning of an array and returns the new length of the array.

Method of Array
Implemented in: JavaScript 1.2, NES 3.0
ECMA Version: ECMA-262 Edition 3

Syntax

arrayName.unshift(element1, ..., elementN)

Parameters

element1, ..., elementN 
The elements to add to the front of the array.

Examples

Example: Adding elements to an array

The following code displays the myFish array before and after adding elements to it.

myFish = ["angel", "clown"];
document.writeln("myFish before: " + myFish);
unshifted = myFish.unshift("drum", "lion");
document.writeln("myFish after: " + myFish);
document.writeln("New length: " + unshifted);

This example displays the following:

myFish before: ["angel", "clown"]
myFish after: ["drum", "lion", "angel", "clown"]
New length: 4

See Also

pop, push, shift

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Adds one or more elements to the beginning of an array and returns the new length of the array.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.2, NES 3.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262 Edition 3</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
arrayName.unshift(<i>element1</i>, ..., <i>elementN</i>)
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>element1, ..., element<i>N</i></code> </dt><dd> The elements to add to the front of the array.
</dd></dl>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Adding_elements_to_an_array"> Example: Adding elements to an array </h4>
<p>The following code displays the <code>myFish</code> array before and after adding elements to it.
</p>
<pre>myFish = ["angel", "clown"];
document.writeln("myFish before: " + myFish);
unshifted = myFish.unshift("drum", "lion");
document.writeln("myFish after: " + myFish);
document.writeln("New length: " + unshifted);
</pre>
<p>This example displays the following:
</p>
<pre>myFish before: ["angel", "clown"]
myFish after: ["drum", "lion", "angel", "clown"]
New length: 4
</pre>
<h3 name="See_Also"> See Also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Objects/Array/pop">pop</a>, 
<a href="en/Core_JavaScript_1.5_Reference/Objects/Array/push">push</a>, 
<a href="en/Core_JavaScript_1.5_Reference/Objects/Array/shift">shift</a>
</p>
Revert to this revision