OneCompiler

[JavaScript] How to insert an element at the end of the array?

I want to insert a new element at the ending of an array, how to do that in Javascript?

1 Answer

4 years ago by

You can use splice() function to add elements at the end of an array

let numbers = [1,2,3,4,5,6,7,8,9];
numbers.splice(numbers.length, 0, 10); //Syntax: array.splice(index, howmany items to be removed, item1, ....., itemn)

Try checking the result here: https://onecompiler.com/javascript/3xmtakwcv

4 years ago by Meera