Last Updated : 13 Nov, 2024
To insert elements at the end of a JS array, the JavaScript push() method is used.
JavaScript
let a = [10, 20, 30, 40];
a.push(50);
console.log(a);
[ 10, 20, 30, 40, 50 ]Using Built-In Methods
The JavaScript push() method is used to insert elements at the end of the array.
JavaScript
let a = [10, 20, 30, 40];
let ele = 50;
a.push(ele);
console.log(a);
[ 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.
JavaScript
let a = [10, 20, 30, 40];
let ele = 50;
a[a.length] = ele;
console.log(a);
[ 10, 20, 30, 40, 50 ]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4