JavaScript Program to Add Element to Start of an Array

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Add Element to Array Using unshift()

Output

[4, 1, 2, 3]

In the above program, the new element is added to the array variable using the unshift() method.

The unshift() method adds a new element at the beginning of an array.


Example 2: Add Element to Array Using splice()

Output

[4, 1, 2, 3]

In the above program, the splice() method is used to add a new element to an array.

In the splice() method,

  • The first argument is the index of an array where you want to add an element.
  • The second argument is the number of elements that you want to remove from the index element.
  • The third argument is the element that you want to add to the array.

Example 3: Add Element to Array Using Spread Operator

Output

[4, 1, 2, 3]

In the above program, the spread operator ... is used to add a new element to the beginning of an array.

arr = [4, ...arr]; takes first element as 4 and the rest elements are taken from array.


Example 4: Add Element to Array Using concat()

Output

[4, 1, 2, 3]

In the above program, the concat() method is used to add a new element to an array.

The concat() method combines two arrays into one.