I mean it's the opposite of shift() , so if you're struggling to remember what it does, maybe that's a useful cognitive hook to hang it on? Answer from pookage on reddit.com
🌐
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().
🌐
W3Schools
w3schools.com › jsref › jsref_unshift.asp
JavaScript Array unshift() Method
The unshift() method overwrites the original array. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make ...
Discussions

Who thought "unshift()" was a good name? Why?
I mean it's the opposite of shift() , so if you're struggling to remember what it does, maybe that's a useful cognitive hook to hang it on? More on reddit.com
🌐 r/learnjavascript
86
32
December 20, 2023
javascript array unshift some elements - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
Basic JavaScript - Manipulate Arrays With unshift Method
Your code so far // Setup const ... x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Challenge: Basic JavaScript - Manipulate Arrays With unshift Method Link to the challenge:... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
August 22, 2023
arrays - Time complexity of unshift() vs. push() in Javascript - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
CodingNomads
codingnomads.com › javascript-array-unshift-shift-pop-push
JavaScript Array Essentials: Using pop, push, shift, and unshift
The names "shift" and "unshift" ... one "shifts" elements out of the array, while the other "unshifts" or pushes elements into the array from the start....
🌐
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...

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 (*_*)

Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript - Manipulate Arrays With unshift Method - JavaScript - The freeCodeCamp Forum
August 22, 2023 - Tell us what’s happening: Describe your issue in detail here. Your code so far // Setup const myArray = [["John", 23], ["dog", 3]]; myArray.shift(); // Only change code below this line myArray.unshift[("Paul" , 35)];…
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/

🌐
DEV Community
dev.to › tochi_ › javascript-array-methods-under-the-hood-unshift-and-shift-explained-3gd
JavaScript Array Methods Under the Hood: Unshift and Shift Explained - DEV Community
July 31, 2025 - If you have an array and you want to add something to the front, i.e index 0, JavaScript has to move every other item one step forward to make space. Imagine shifting people in a line forward to make room for one more at the start. That takes time. Let’s look at what unshift() does under the hood.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › what-is-the-difference-between-unshift-and-push-method-in-javascript
What is the difference between unshift() and Push() method in JavaScript? - GeeksforGeeks
July 23, 2025 - JavaScript Unshift() method is very much similar to the push() method but the difference is that the unshift() method adds the elements at the very beginning of the array whereas the push() method adds at the end of the array.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript array unshift method
JavaScript Array Unshift Method
September 1, 2008 - In JavaScript, the Array.unshift() method is used to add one or more elements to the start of an array and returns the new length of the array.
🌐
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 - These add and remove items to and from the beginning of an array, respectively. Unshift() is a pretty awesome function; so, when I realized yesterday that I tend to use the more complex splice() method in order to achieve unshift() functionality, I was saddened.
🌐
Meticulous
meticulous.ai › blog › javascript-unshift-complete-guide
JavaScript Unshift | In-Depth Guide & Tutorial
Unshift is a method for adding elements to the beginning of an array or an object resembling an array. It's one of the most commonly used JavaScript methods when dealing with arrays and has the signature of unshift(element1, element2... elementN).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-unshift-method
JavaScript Array unshift() Method - GeeksforGeeks
July 15, 2024 - JavaScript Array unshift() Method is used to add one or more elements to the beginning of the given array.
🌐
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 - The unshift() method is like the push() method, only it works at the beginning of the array. The unshift() method can prepend one or more elements to the beginning of an array.
🌐
Scaler
scaler.com › home › topics › javascript array unshift() method
JavaScript Array unshift() Method - Scaler Topics
February 23, 2024 - Array.unshift() is a JavaScript method that adds one or more elements to the beginning of an array and returns the new length of the array. It modifies the original array rather than creating a new one.
🌐
Programiz
programiz.com › javascript › library › array › unshift
JavaScript Array unshift()
var languages = ["JavaScript", "Python", "Java", "Lua"]; var count = languages.unshift("C++"); console.log(languages); // [ 'C++', 'JavaScript', 'Python', 'Java', 'Lua' ] console.log(count); // 5 var priceList = [12, 21, 35];
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Array methods
The JavaScript language · Data types · December 31, 2023 · Arrays provide a lot of methods. To make things easier, in this chapter, they are split into groups. We already know methods that add and remove items from the beginning or the end: arr.push(...items) – adds items to the end, arr.pop() – extracts an item from the end, arr.shift() – extracts an item from the beginning, arr.unshift(...items) – adds items to the beginning.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › unshift
JavaScript Array unshift() - Add Elements to Start | Vultr Docs
November 29, 2024 - Utilize the unshift() method in JavaScript to add elements to the start of an array effectively. Whether adding a single item or multiple items, this method adjusts the array's indices and updates its length accordingly.
🌐
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon"); Try it Yourself » · Array elements are accessed using their index number: ... const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits[fruits.length] = "Kiwi"; Try it Yourself » · ECMAScript 5 (JavaScript 2009) added the new method Array.isArray() to JavaScript: