JavaScript (ES2026) reference · Array Methods
JavaScript Array unshift() Method
Add one or more elements to the start of the array and return the new length. Changes the array in place.
JavaScript (ES2026) Since ES3
Syntax
array.unshift(element1, /* …, */ elementN)
Parameters
| Parameter | Description |
|---|---|
elements |
Elements to add at the front. |
Return value
The new length.
Examples
const list = ["b", "c"];
console.log(list.unshift("a"), list);
Output
3 [ 'a', 'b', 'c' ]
const recent = ["page2", "page1"];
recent.unshift("page3");
console.log(recent.slice(0, 2)); // the two most recent
Output
[ 'page3', 'page2' ]
Version history
| Version | Change |
|---|---|
| JavaScript ES3 | Added |
Related
array.shift()· Remove the first element of the array and return it, moving the other elements down. Returns undefined if empty.array.push()· Add one or more elements to the end of the array and return the new length. Changes the array in place.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial