You can actually accomplish this using the .concat method if you call it on an array of elements you want to put at the front of the new array.

Working Example:

var a = [1, 2, 3];
var b = [0].concat(a);
console.log(a);
console.log(b);

Alternately, in ECMAScript 6 you can use the spread operator.

Working Example (requires modern browser):

var a = [1, 2, 3]
var b = [0, ...a];
console.log(a);
console.log(b);

Answer from Alexander O'Mara on Stack Overflow
🌐
Meticulous
meticulous.ai › blog › javascript-unshift-complete-guide
JavaScript Unshift | In-Depth Guide & Tutorial
If you are regularly prepending ... time complexity cost, such as a linked list or circular buffer. One strategy for avoiding the mutation is to make use of JavaScript's spread operator, which 'unpacks' the values within ...
🌐
Medium
medium.com › an-idea › javascript-arrays-push-pop-shift-unshift-adc8fb815fc0
JavaScript Arrays: push(), pop(), shift() & unshift() | by Amanda M Johnson | An Idea (by Ingenious Piece) | Medium
October 10, 2021 - JavaScript Arrays: push(), pop(), shift() & unshift() When working with arrays, it is important to understand the different types of methods and how they transform your data. push(), pop(), shift()
🌐
Reddit
reddit.com › r/learnjavascript › who thought "unshift()" was a good name? why?
r/learnjavascript on Reddit: Who thought "unshift()" was a good name? Why?
December 20, 2023 -

EDIT: This is just venting, not confusion on how the word is technically used by JS :D

OK, this is why I keep starting and stopping learning JS- reserved words with extremely unrelated English meanings. "Unshift" means a previous shift occurred. That's the only meaning for it in English. But JS decided to use it as a prepend array method. Argh!!

That is all...

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › unshift
Array.prototype.unshift() - JavaScript | MDN
The unshift() method reads the length property of this. It shifts all indices in the range 0 to length - 1 right by the number of arguments (incrementing their values by this number). Then, it sets each index starting at 0 with the arguments passed to unshift().
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-perform-unshift-operation-without-using-unshift-method-in-javascript
How to perform unshift operation without using unshift() method ...
August 5, 2025 - Performing an unshift operation in JavaScript without unshift() involves using array concatenation or the ES6 spread operator.
Find elsewhere
🌐
CodingNomads
codingnomads.com › javascript-array-unshift-shift-pop-push
JavaScript Array Essentials: Using pop, push, shift, and unshift
While it's true that .shift() and .unshift() are less efficient for large arrays, the difference is negligible for most practical applications. Students should focus on understanding how these methods work and how they can be used effectively in various scenarios as good abstractions. Premature optimization can lead to overly complex code, and often, the simplest solution is the best one to start with. JavaScript's .push() and .unshift() methods are versatile tools that can handle more than just single arguments, unlike their no-argument counterparts, .pop() and .shift().
🌐
GeeksforGeeks
geeksforgeeks.org › what-is-the-difference-between-unshift-and-push-method-in-javascript
What is the difference between unshift() and Push() method in JavaScript? | GeeksforGeeks
January 10, 2023 - In JavaScript, Object.freeze makes an object immutable, preventing any changes to existing properties and values. Object.seal allows changes to existing properties but prevents adding or removing properties.
🌐
SheCodes
shecodes.io › athena › 18575-difference-between-array-shift-and-array-unshift-in-javascript
[JavaScript] - Difference between array.shift() and | SheCodes
Learn how to use array.shift() and array.unshift() methods in JavaScript to manipulate elements in an array · How do I make a javascript event where you click on a thumbnail image and it pops up with a full sized image
Top answer
1 of 9
70

push() is faster.

js>function foo() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.unshift(1); return((new Date)-start)}
js>foo()
2190
js>function bar() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.push(1); return((new Date)-start)}
js>bar()
10

function foo() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.unshift(1); return((new Date)-start)}
console.log(foo())

function bar() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.push(1); return((new Date)-start)}
console.log(bar());


Update

The above does not take into consideration the order of the arrays. If you want to compare them properly, you must reverse the pushed array. However, push then reverse is still faster by ~10ms for me on chrome with this snippet:

var a=[]; 
var start = new Date; 
for (var i=0;i<100000;i++) {
  a.unshift(1);
}
var end = (new Date)-start;
console.log(`Unshift time: ${end}`);

var a=[];
var start = new Date;
for (var i=0;i<100000;i++) {
  a.push(1);
}

a.reverse();
var end = (new Date)-start;
console.log(`Push and reverse time: ${end}`);

2 of 9
29

The JavaScript language spec does not mandate the time complexity of these functions, as far as I know.

It is certainly possible to implement an array-like data structure (O(1) random access) with O(1) push and unshift operations. The C++ std::deque is an example. A Javascript implementation that used C++ deques to represent Javascript arrays internally would therefore have O(1) push and unshift operations.

But if you need to guarantee such time bounds, you will have to roll your own, like this:

http://code.stephenmorley.org/javascript/queues/

🌐
Bennadel
bennadel.com › blog › 1796-javascript-array-methods-unshift-shift-push-and-pop.htm
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
April 21, 2020 - This question probably doesn't have any real world application, but I have a JavaScript exercise that asks me to .unshift a variable to the front of my array then simultaneously remove it with the .shift method.
Top answer
1 of 6
4

There are many ways to do this, but not as many that preserve the original aArr reference (e.g. modify the actual aArr array without creating a new one). Here's one way:

aArr.splice(0, bArr.length);            // remove front-most bArr.length items from aArr
aArr.unshift.apply(aArr, bArr);         // insert bArr items in front of aArr

This removes the first bArr.length items form aArr and then adds the bArr items to the front of aArr, all the while preserving the original aArr reference (e.g. not replacing it with a new array).


It can also be done in one .splice(), but that requires building a temporary array to pass to .splice.apply() which didn't seem worth it since that makes an entirely new array just to save one line of code. In any case, that would look like this:

aArr.splice.apply(aArr, [0, bArr.length].concat(bArr));

If you really want max "efficiency" in terms of performance rather than in terms of lines of code, then you will probably need to do performance benchmarks using a tool like jsperf and test in multiple browsers. It may be that simply copying over the bArr items into aArr is the most "efficient" because that has the fewest array manipulations. To know for sure, you would have to measure actual performance at your typical array sizes across several browsers.

For pure performance, you should test this vs the options above:

for (var i = 0, len = bArr.length; i < len; i++) {
    aArr[i] = bArr[i];
}

This has the advantage that it does not create any temporary arrays and does not have to shift the items in aArr around. It has the disadvantage of running the loop in plain javascript, not in native array manipulation code.


It appears that the last option of just copying elements over is 7x faster in Chrome, 10x faster in IE11 and even more of a difference in Firefox.

See the jsperf here: http://jsperf.com/array-shift-vs-copy

2 of 6
1
function prependArray(a, b) {
    return a.splice.apply(a, [0, b.length].concat(b))
}

Thanks The Paramagnetic Croissant (*_*)

🌐
Bennadel
bennadel.com › blog › 2300-creating-a-javascript-shim-for-array-unshift-method.htm
Creating A JavaScript Shim For Array Unshift() Method
April 21, 2020 - Ben Nadel demonstrates how to create a JavaScript shim for the Array unshift() method in browsers that only support the Array splice() method.
🌐
Career Karma
careerkarma.com › blog › javascript › javascript shift and javascript unshift: a complete guide
JavaScript Shift and JavaScript Unshift: A Complete Guide
December 1, 2023 - JavaScript shift() removes an element at a specified position and shifts the remaining elements up. The JavaScript unshift() function does the opposite.
🌐
Medium
medium.com › @andrewasmit › array-method-madness-a-review-of-push-pop-shift-unshift-slice-splice-d51c84ed778b
Array Method Madness: a review of .push() .pop() .shift() .unshift() .slice() & .splice() | by Andrew Smit | Medium
October 26, 2022 - Just like with the .push() method, if I take the code where I invoke .unshift() and save that to a variable, the variable’s value will be a number that shows the length of the modified array.