JavaScript Array.prototype.unshift() Method
The unshift() method adds one or more elements to the start of an array and returns the new length.
Description
The unshift() method adds elements to the beginning of an array, moving the existing elements up, and returns the new length. It changes the array in place. To add to a copy instead, use [item, ...arr].
Like push(), it accepts several elements and adds them in the order given, so arr.unshift(1, 2) puts 1 first and 2 second. The return value is the new length, not the array.
Because every existing element has to move up by one position, unshift() gets slower as arrays get larger, so it isn't ideal for adding many items one at a time to a big array. Building the list in reverse with push() and reversing it once at the end, or creating a new array with spread syntax, is often faster.
Syntax
array.unshift(element1, element2, ...)Parameters
- element1, element2, ...The elements to add to the start of the array.
Return Value
The new length of the array.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.unshift() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





