JavaScript array is a single variable that is used to store the elements or a group of values. You can add or remove elements from the array in any position. In this article, we will discuss different ways to add elements to a JavaScript array.

Methods to Add Elements to a JavaScript Array
Following are the methods that can be used to add elements into a JavaScript array:
Method 1: Using Push() Method
In this methos, we will use the push() method to add one or more elements to the end of an array.
Example: In this example, the push() method is used to add multiple elements at the end of the array.
Javascript
const myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray);
myArray.push(5, 6);
console.log(myArray);
|
Output
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 4, 5, 6 ]
Method 2: Using concat() Method
In this approach, the concat() method is used to create a new array by combining existing arrays with additional elements.
Example: The below code implements the concat method to add elements to an JavaScript array.
Javascript
const myArray1 = ['JavaScript', 'GeeksforGeeks'];
const myArray2 = ['TypeScript', 'ReactJs', 'Angular'];
const newArray = myArray1.concat(myArray2);
console.log(newArray);
|
Output
[ 'JavaScript', 'GeeksforGeeks', 'TypeScript', 'ReactJs', 'Angular' ]
Method 3: Using Splice() Method
The splice() method can be used to add or remove elements from a specific index in an array. We need to set the second argument of the splice method to 0 for inserting elements into an array using it.
Example: The below example shows the use of the splice method to add elements to an array.
Javascript
const myArray = [1, 2, 3];
myArray.splice(1, 0, 4);
console.log(myArray);
|
Method 4: Using Spread Operator
The spread operator can also be used to extend an array and create a new array with new elements.
Example: The below example illustrates the use of spread operator to add elements to an array.
Javascript
const myArray = [1, 2, 3];
const newArray = [...myArray, 4, 5];
console.log(newArray)
|
Method 5: Using Unshift() Method
The unshift() method adds one or more elements to the beginning of an array.
Example: In the below code example the unshift() method is used to add the elements into an array.
Javascript
const myArray = [4, 5, 6];
myArray.unshift(3);
console.log(myArray);
myArray.unshift(1, 2);
console.log(myArray);
|
Output
[ 3, 4, 5, 6 ]
[ 1, 2, 3, 4, 5, 6 ]
Method 6: Using Length Property
The array length property in JavaScript is used to set or return the number of elements in an array. This property can also be used to add elements at the end of the array.
Example: The below example will show how you can use the length property to add elements at the end of the array in JavaScript.
Javascript
const myArray = [1, 2, 3];
console.log(myArray.length);
myArray[myArray.length] = 4;
console.log(myArray);
console.log(myArray.length);
|
Output
3
[ 1, 2, 3, 4 ]
4
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
04 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...