Javascript add element to beginning of array – 4 ways

A general requirement while developing in javascript is to add an element to the beginning of an array. This article will discuss adding an item to the start of an array using simple methods and example illustrations.

Table of Contents:-

Add an element to start of array using concat()

Javascript’s concat() method is used to merge more than one array. This method does not change the contents of the original arrays but instead returns a new one.

Example:-

Add an element with value 1 to the beginning of the array  [4,9,16,25]

Code:-

let  numArray = [4,9,16,25];
let element = 1;
console.log("Array Elements Before Adding: " + numArray);
numArray = [element].concat(numArray) 
console.log("Array Elements After Adding : " + numArray);

Output:-

Array Elements Before Adding: 4,9,16,25
Array Elements After Adding : 1,4,9,16,25

Add an element to start of array using unshift()

Javascript’s unshift() method adds one or more elements at the start of an array. The unshift() method returns the new length of the array.

Example:-

Add an element with value 1 to the beginning of the array  [4,9,16,25]

Code:-

let numArray = [4,9,16,25];
let element = 1;
console.log("Array Elements Before Adding: " + numArray);
numArray.unshift(element);
console.log("Array Elements After Adding : " + numArray);

Output:-

Array Elements Before Adding: 4,9,16,25
Array Elements After Adding : 1,4,9,16,25

Add an element to start of array using splice()

Javascript’s splice(start, deleteCount, item1, item2….) method is used to modify the elements of an array. The splice() method can remove, replace or/and add new elements to the array.

  • start: is the index from where the change in the array needs to be done.
  • deleteCount: is the number of elements to be removed from the array.
  • item1,item2,… : are the elements that need to be added.

Example:-

Add an element with value 1 to the beginning of the array  [4,9,16,25]

Code:-

let numArray = [4,9,16,25];
let element = 1;
console.log("Array Elements Before Adding: " + numArray);
numArray.splice(0, 0, element)
console.log("Array Elements After Adding : " + numArray);

Output:-

Array Elements Before Adding: 4,9,16,25
Array Elements After Adding : 1,4,9,16,25

Explanation:-

In the above code numArray.splice(0, 0, element) is used, which says to change the numArray from 0th index, delete 0 elements and add the item1 that is specified in the element variable.

Add an element to start of array spread syntax(…)

The spread syntax in javascript ES6 is denoted by three dots (…) and is used to spread out the elements of an iterable object such as an array, map, set to places where one or more arguments are expected.

Example:-

Add an element with value 1 to the beginning of the array  [4,9,16,25]

Code:-

let numArray = [4,9,16,25];
let element = 1;
console.log("Array Elements Before Adding: " + numArray);
numArray = [element, ...numArray];
console.log("Array Elements After Adding: " + numArray);

Output:-

Array Elements Before Adding: 4,9,16,25
Array Elements After Adding : 1,4,9,16,25

Read More:

I hope this article helped you to add an element to the beginning of an array in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top