A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript-insert-elements-at-the-end-of-js-array/ below:

JavaScript - Insert Elements at the End of JS Array

JavaScript - Insert Elements at the End of JS Array

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);

Output
[ 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);

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.

JavaScript
let a = [10, 20, 30, 40];
let ele = 50;
a[a.length] = ele;
console.log(a);

Output
[ 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