JavaScript Array unshift() Method

The JavaScript Array.unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. This method modifies the original array by inserting elements at the start, shifting existing elements to higher indices.

For example, array.unshift(element1, element2, ...) is useful when you need to add elements to the beginning of an array and update its length accordingly.

Syntax

The syntax for the Array.unshift() method is:

plaintext
array.unshift(element1, element2, ..., elementN)

Parameters

The Array.unshift() method accepts one or more parameters:

  • element1, element2, … (Required): The elements to be added to the beginning of the array.

Return Value

The new length of the array after adding the specified elements.

Examples of JavaScript Array.unshift() Method

Example 1: Adding One Element to the Beginning of an Array

You can use unshift() to add a single element to the beginning of an array.

javascript
const fruits = ['banana', 'mango'];
const newLength = fruits.unshift('apple');
console.log(fruits);     // Output: ['apple', 'banana', 'mango']
console.log(newLength);  // Output: 3

Explanation: The unshift() method adds 'apple' to the beginning of the fruits array, making the new array ['apple', 'banana', 'mango']. It returns the new length, which is 3.

Example 2: Adding Multiple Elements to the Beginning of an Array

You can use unshift() to add multiple elements to the start of an array.

javascript
const numbers = [10, 20, 30];
const newLength = numbers.unshift(1, 2, 3);
console.log(numbers);     // Output: [1, 2, 3, 10, 20, 30]
console.log(newLength);   // Output: 6

Explanation: The unshift() method adds 1, 2, and 3 to the beginning of the numbers array, shifting the existing elements to higher indices. The method returns the new length of the array, which is 6.

Example 3: Using unshift() on an Empty Array

When using unshift() on an empty array, it adds elements and returns the new length.

javascript
const emptyArray = [];
const newLength = emptyArray.unshift('apple', 'banana');
console.log(emptyArray);  // Output: ['apple', 'banana']
console.log(newLength);   // Output: 2

Explanation: The unshift() method adds 'apple' and 'banana' to the empty array and returns the new length of 2.

Example 4: Using unshift() with an Array of Objects

The unshift() method can be used to add objects to the beginning of an array of objects.

javascript
const users = [{ name: 'Bob' }, { name: 'Charlie' }];
const newUser = { name: 'Alice' };
users.unshift(newUser);
console.log(users);  // Output: [{ name: 'Alice' }, 
								 { name: 'Bob' }, 
								 { name: 'Charlie' }]

Explanation: The unshift() method adds the object { name: 'Alice' } to the start of the users array, moving the existing objects to higher indices.

Example 5: Chaining unshift() with Other Methods

You can combine unshift() with other array methods like pop() or shift() to manipulate arrays dynamically.

javascript
const queue = ['task1', 'task2'];
queue.unshift('task0');
queue.pop();
console.log(queue);  // Output: ['task0', 'task1']

Explanation: The unshift() method adds 'task0' to the beginning of the queue array, and then pop() removes the last element ('task2'), resulting in ['task0', 'task1'].

JavaScript

2478

518

Related Articles