JavaScript Array unshift() Tutorial

In this section, we will learn what the Array unshift() method is and how to use it in JavaScript.

JavaScript Arrays unshift() Method

We can use the `unshift()` method when we want to add new elements to the beginning of an array.

Note: if you want to add elements to the end of an array, use the Array push() method.

Array unshift() Syntax:

array.unshift(item1, item2, ..., itemX)

Array unshift() Method Parameters:

The arguments to the `unshift()` method are the elements that want to add to the target array.

Array unshift() Method Return Value:

The return value of this method is the new length of the target array.

Example: JavaScript add item to beginning of array

const arr = ["Tesla","Google"];

const length = arr.unshift("Microsoft","Amazon","Facebook","Twitter");

console.log(`The new length of the array is: ${length}`);

console.log(arr);

Output:

The new length of the array is: 6

["Microsoft", "Amazon", "Facebook", "Twitter", "Tesla", "Google"]

Leave a Reply