Question
I need to add, or prepend, an element to the beginning of a JavaScript array.
For example, if the array is:
[23, 45, 12, 67]
and the response from an AJAX call is 34, I want the updated array to become:
[34, 23, 45, 12, 67]
Right now, I am considering this approach:
var newArray = [];
newArray.push(response);
for (var i = 0; i < theArray.length; i++) {
newArray.push(theArray[i]);
}
theArray = newArray;
Is there a better way to do this? Does JavaScript provide a built-in method for adding elements to the beginning of an array?
Also, since my current approach is O(n), I would be interested in understanding whether there are better implementations and what their trade-offs are.
Short Answer
By the end of this page, you will understand how to add elements to the beginning of a JavaScript array, when to use unshift(), how it compares with alternatives like spread syntax and concat(), and why prepending to arrays usually remains an O(n) operation.
Concept
In JavaScript, arrays have built-in methods for adding and removing elements. When you want to place a new value at the start of an array, the standard built-in method is unshift().
const arr = [23, 45, 12, 67];
arr.unshift(34);
console.log(arr); // [34, 23, 45, 12, 67]
This matters because adding to the beginning of an array is a very common operation in real applications:
- showing newest notifications first
- adding recent messages to a feed
- maintaining a list of latest results
- inserting priority items at the front
However, there is an important performance idea behind this: JavaScript arrays are index-based. If you insert an item at index 0, the existing items usually need to shift one position to the right.
For example:
23moves from index0to145moves from index1to212moves from index to
Mental Model
Think of a JavaScript array like a row of numbered lockers:
- locker
0contains23 - locker
1contains45 - locker
2contains12 - locker
3contains67
If you want to put 34 into locker 0, the current contents cannot stay where they are. Every existing item must move one locker to the right.
So prepending is like inserting a new person at the front of a line: everyone already in line has to step back one position.
That is why adding at the beginning is more work than adding at the end. Appending with push() is usually easier because no existing elements need to move.
Syntax and Examples
The most direct built-in method is unshift().
array.unshift(element1, element2, ...)
It adds one or more elements to the beginning of an array and returns the new length.
Basic example
const numbers = [23, 45, 12, 67];
const newLength = numbers.unshift(34);
console.log(numbers); // [34, 23, 45, 12, 67]
console.log(newLength); // 5
Explanation:
unshift(34)inserts34at the start- the original array is modified
- the method returns the array's new length
Add multiple elements
const numbers = [23, 45, 12, 67];
numbers.unshift(1, , );
.(numbers);
Step by Step Execution
Consider this example:
const arr = [23, 45, 12, 67];
arr.unshift(34);
console.log(arr);
Here is what happens step by step:
Step 1: Create the array
const arr = [23, 45, 12, 67];
Memory conceptually looks like this:
| Index | Value |
|---|---|
| 0 | 23 |
| 1 | 45 |
| 2 | 12 |
| 3 | 67 |
Step 2: Call unshift(34)
Real World Use Cases
Prepending values to arrays appears in many common situations.
Activity feeds
A new notification arrives and should appear first:
const notifications = ["Older alert", "Yesterday's alert"];
notifications.unshift("New alert");
Chat applications
If messages are displayed with newest first, a newly received message may be inserted at the front:
const messages = ["How are you?", "Hello"];
messages.unshift("Latest message");
Search history
Recent searches are often shown first:
const searches = ["javascript map", "css grid"];
searches.unshift("array unshift");
Logging and monitoring dashboards
Incoming events may be added to the start of a list so the most recent issue is visible immediately.
Data processing scripts
You may prepend a header row or a high-priority item before processing a list.
tasks = [, ];
tasks.();
Real Codebase Usage
In real projects, developers choose between mutating and non-mutating approaches depending on the situation.
1. Direct mutation with unshift()
Common in simple scripts or local state where changing the original array is fine.
queue.unshift(newItem);
Use this when:
- you own the array
- mutation is acceptable
- you want the simplest built-in method
2. Immutable update with spread syntax
Very common in React, Redux, and other state-management patterns.
const updatedItems = [newItem, ...items];
Use this when:
- you should not mutate existing state
- change detection depends on a new array reference
- you want predictable state updates
3. Guard clauses before prepending
Developers often validate data first.
if (response == null) {
return;
}
items.unshift(response);
This prevents invalid values from being inserted.
4. Keeping arrays limited in size
A common pattern is prepend, then trim.
Common Mistakes
Here are common beginner mistakes when adding elements to the beginning of an array.
Mistake 1: Expecting unshift() to return the array
Broken code:
const arr = [23, 45];
const result = arr.unshift(34);
console.log(result); // 3, not [34, 23, 45]
Why it happens:
unshift()returns the new length- it does not return the updated array
Correct version:
const arr = [23, 45];
arr.unshift(34);
console.log(arr); // [34, 23, 45]
Mistake 2: Mutating an array when you wanted a new one
Broken code:
const original = [23, 45];
const alias = original;
alias.unshift();
.(original);
Comparisons
Here is how the main options compare.
| Approach | Mutates original array? | Returns | Typical use |
|---|---|---|---|
unshift(value) | Yes | New length | Simple in-place prepend |
[value, ...array] | No | New array | Immutable updates |
[value].concat(array) | No | New array | Older non-mutating style |
| Manual loop | Usually no, unless assigned back | Depends on code | Rarely needed for this task |
unshift() vs push()
Cheat Sheet
// Mutating prepend
arr.unshift(value);
// Mutating prepend of multiple values
arr.unshift(v1, v2, v3);
// Non-mutating prepend with spread
const updated = [value, ...arr];
// Non-mutating prepend with concat
const updated = [value].concat(arr);
Key rules
unshift()adds elements to the start of an arrayunshift()mutates the arrayunshift()returns the new lengthpush()adds to the end, not the beginning- Prepending to a normal array is usually O(n)
Quick examples
const a = [2, 3];
a.unshift(1);
console.log(a); // [1, 2, 3]
const a = [2, 3];
b = [, ...a];
.(a);
.(b);
FAQ
Is there a built-in JavaScript method to add an item to the beginning of an array?
Yes. Use array.unshift(value).
Does unshift() change the original array?
Yes. unshift() mutates the existing array.
What does unshift() return in JavaScript?
It returns the new length of the array, not the array itself.
How do I prepend without modifying the original array?
Use spread syntax or concat():
const updated = [value, ...original];
Is prepending to a JavaScript array O(1)?
Usually no. It is generally O(n) because existing elements need to shift or be copied.
Can I add multiple elements to the beginning at once?
Yes.
arr.unshift(1, 2, 3);
Should I use unshift() in React state updates?
Mini Project
Description
Build a small recent-search tracker. Each time a user performs a search, the newest search term should appear at the beginning of the list. This is a practical example of prepending items to an array and limiting the list to a fixed size.
Goal
Create a function that adds a new search term to the beginning of an array and keeps only the 5 most recent searches.
Requirements
- Start with an array of existing search terms.
- Add each new search term to the beginning of the array.
- Keep only the 5 most recent terms.
- Ignore empty search strings.
- Print the updated list after each insert.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.