Insert at the End of an Array in JavaScript
Last Updated :
17 Nov, 2024
Here are different ways to add an item at the end of array
1. Using push() Method - Most Used :
The JavaScript Array push() Method is used to add one or more values to the end of the array. This method changes the length of the array by the number of elements added to the array.
JavaScript
const a = [1, 2, 3];
a.push(4);
console.log(a);
// adding multiple element i the last index of array
const b = [10, 20, 30, 40, 50];
b.push(60, 70);
console.log(b);
Output[ 1, 2, 3, 4 ]
[
10, 20, 30, 40,
50, 60, 70
]
2. Using Spread (...) - For adding Multiple Elements
Using the spread (...) operator, you can add elements to the end of an array by creating a new array and combining the original array with the new elements.
JavaScript
let a = [1, 2, 3];
let b = [...a, 4, 5];
console.log(b);
3. Using concat() - For adding multiple elements
In this approach, Using the concat() method, create a new array by combining the original array with additional elements. It does not modify the original array, giving a clean way to add elements.
JavaScript
const a = [1, 2, 3];
const b = a.concat(4, 5);
console.log(b);
4. Using the length Property - Can add empty spaces
In this approach, we are Using the length property, you can add elements to the end of an array by assigning values to indexes equal to the current length, effectively extending the array with new elements.
JavaScript
let a = [1, 2, 3, 7];
a[a.length] = 4;
a[a.length] = 5;
console.log(a);
Output[ 1, 2, 3, 7, 4, 5 ]
5. Using splice() Method
Using the splice() method, you can add elements to the end of an array by specifying arr.length as the index and setting the number of elements to remove to 0. It directly modifies the original array.
JavaScript
let a = [1, 2, 3];
a.splice(a.length, 0, 4, 5);
console.log(a);
6. Using a loop
Using a loop, you can add multiple elements to the end of an array in JavaScript. Iterate through the elements to be added and assign each to the next available index in the array using its length. This appends the new elements sequentially.
JavaScript
let a = [1, 2, 3];
let b = [4, 5];
for (let i = 0; i < b.length; i++) {
a[a.length] = b[i];
}
console.log(a);
7. Using apply() Method
The apply() method in JavaScript allows you to call a function with a given this value, and arguments provided as an array. You can use this to add elements to the end of an array by combining the original array with new elements.
JavaScript
let a = [1, 2, 3];
let b = [4, 5];
Array.prototype.push.apply(a, b);
console.log(a);
Importance of Adding Elements to an Array
Adding elements to an array is crucial for:
- Building and modifying datasets dynamically.
- Implementing queue and stack operations.
- Enhancing functionality in applications that require data storage and retrieval.
Similar Reads
How to Add Elements to a JavaScript Array? Here are different ways to add elements to an array in JavaScript.1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array.Syntaxarray.push( element1, element2, . . ., elementN );JavaScriptconst arr = [10, 20, 30, 40]; arr.push(
3 min read
Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J
3 min read
Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element
2 min read
Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple",
6 min read
Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
3 min read
How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt
3 min read
Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element
2 min read
Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3
3 min read
JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2
2 min read
Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J
3 min read