How to push to an array with JavaScript?

Estimated read time 1 min read

You can use the Array.prototype.push() method in JavaScript to add elements to the end of an array. Here’s an example:

let arr = [1, 2, 3];
arr.push(4);

console.log(arr);

This will output [1, 2, 3, 4].

The push() method adds elements to the end of the array, and returns the new length of the array. So, you can use it in a chain, like this:

let arr = [1, 2, 3];
arr.push(4).push(5);

console.log(arr);

This will output [1, 2, 3, 4, 5].

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply