JavaScript Program to Add Element to Start of an Array
JavaScript is widely used because it allows programmers to make dynamic and interactive websites. The capability to add elements to an array is one of JavaScript's most powerful capabilities. This article explains the JavaScript code required to append a new item to the front of an existing array. Well, so let's begin!
We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.
Program to Add Element to Start of an Array Using unshift()
In the program given below, 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.
- The
unshift()
method overwrites the original array.
// JavaScript Program to add an element at the start of an array
function addElement(arr) {
// adding new array element
arr.unshift(4);
console.log(arr);
}
const array = [3 , 2 , 1];
// calling the function
addElement(array);
[ 4 , 3 , 2 , 1 ]
Program to Add Element to Start of an Array Using splice()
In the program given below, 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.
// JavaScript Program to add an element at the start of an array
function addElement(arr) {
// adding new array element
arr.splice(0,0,4);
console.log(arr);
}
const array = [3, 2, 1];
// calling the function
addElement(array);
[ 4 , 3 , 2 , 1 ]
Program to Add Element to Start of an Array Using Spread Operator
In the program given below, the spread operator [... ]
is used to add a new element to the beginning of an array.
// JavaScript Program to add an element at the start of an array
function addElement(arr) {
// adding new array element
arr = [4 , ...arr ];
console.log(arr);
}
const array = [3, 2, 1];
// calling the function
addElement(array);
[ 4 , 3 , 2 , 1 ]
Program to Add Element to Start of an Array Using concat()
In the program given below, the concat()
method is used to add a new element to an array.
- The
concat()
method combines two arrays into one.
// JavaScript Program to add an element at the start of an array
function addElement(arr) {
// adding new array element
arr = [4].concat(arr);
console.log(arr);
}
const array = [3, 2, 1];
// calling the function
addElement(array);
[ 4 , 3 , 2 , 1 ]
Conclusion
In conclusion, there are multiple ways to do the simple task of appending an element to the front of an array with JavaScript. It is crucial to think about the performance implications of each method while creating the JavaScript code to add an element to the front of an array. Understanding how to append an item to the front of an array in JavaScript is a crucial skill for any programmer who deals with arrays. Following the procedures described here will allow you to quickly and easily populate an array with new elements.