JavaScript - Insert Elements at the End of JS Array
Last Updated :
13 Nov, 2024
Improve
To insert elements at the end of a JS array, the JavaScript push() method is used.
let a = [10, 20, 30, 40];
a.push(50);
console.log(a);
Output
[ 10, 20, 30, 40, 50 ]
Table of Content
Using Built-In Methods
The JavaScript push() method is used to insert elements at the end of the array.
let a = [10, 20, 30, 40];
let ele = 50;
a.push(ele);
console.log(a);
Output
[ 10, 20, 30, 40, 50 ]
Writing Your Own Method
To add an element at the nth index of the array we can simply use the array length property.
let a = [10, 20, 30, 40];
let ele = 50;
a[a.length] = ele;
console.log(a);
Output
[ 10, 20, 30, 40, 50 ]