JavaScript Tutorials

JavaScript Array unshift()
Syntax & Examples

Array.unshift() method

The unshift() method of the Array class in JavaScript adds one or more elements to the front of an array and returns the new length of the array.


Syntax of Array.unshift()

There are 4 variations for the syntax of Array.unshift() method. They are:

1.
unshift()

This method adds no elements to the array and returns the new length of the array.

Returns value of type Number.

2.
unshift(element1)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to add to the front of the array.

This method adds one element to the front of the array and returns the new length of the array.

Returns value of type Number.

3.
unshift(element1, element2)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to add to the front of the array.
element2requiredThe second element to add to the front of the array.

This method adds two elements to the front of the array and returns the new length of the array.

Returns value of type Number.

4.
unshift(element1, element2, /* …, */ elementN)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to add to the front of the array.
element2requiredThe second element to add to the front of the array.
elementNoptionalAdditional elements to add to the front of the array.

This method adds one or more elements to the front of the array and returns the new length of the array.

Returns value of type Number.



✐ Examples

1 Using unshift() method to add one element to an array

In JavaScript, we can use the unshift() method to add one element to the front of an array and return the new length of the array.

For example,

  1. We define an array variable arr with elements [2, 3, 4].
  2. We use the unshift() method to add the element 1 to the front of the array.
  3. The new length of the array is stored in the variable newLength.
  4. We log newLength and the updated array to the console using console.log() method.

JavaScript Program

const arr = [2, 3, 4];
const newLength = arr.unshift(1);
console.log(newLength);  // Output: 4
console.log(arr);       // Output: [1, 2, 3, 4]

Output

4
[1, 2, 3, 4]

2 Using unshift() method to add multiple elements to an array

We can use the unshift() method to add multiple elements to the front of an array and return the new length of the array.

For example,

  1. We define an array variable arr with elements ['c', 'd'].
  2. We use the unshift() method to add the elements 'a' and 'b' to the front of the array.
  3. The new length of the array is stored in the variable newLength.
  4. We log newLength and the updated array to the console using console.log() method.

JavaScript Program

const arr = ['c', 'd'];
const newLength = arr.unshift('a', 'b');
console.log(newLength);  // Output: 4
console.log(arr);       // Output: ['a', 'b', 'c', 'd']

Output

4
['a', 'b', 'c', 'd']

3 Using unshift() method to add elements to an empty array

We can use the unshift() method to add elements to the front of an empty array and return the new length of the array.

For example,

  1. We define an empty array variable arr.
  2. We use the unshift() method to add the elements 1, 2, and 3 to the front of the array.
  3. The new length of the array is stored in the variable newLength.
  4. We log newLength and the updated array to the console using console.log() method.

JavaScript Program

const arr = [];
const newLength = arr.unshift(1, 2, 3);
console.log(newLength);  // Output: 3
console.log(arr);       // Output: [1, 2, 3]

Output

3
[1, 2, 3]

Summary

In this JavaScript tutorial, we learned about unshift() method of Array: the syntax and few working examples with output and detailed explanation for each example.