DEV Community

loading...

how to add items to an array in JS

charlygarcia120 profile image Carlos Alberto ・1 min read

To add elements to an array we will to use the splice function.

.splice(index, howmany, item1, ....., itemX)

index : what position to add/remove items, Use negative values to specify the position from the end of the array
howmany : items to be removed. If set to 0, no items will be removed
item1, ..., itemX Optional. The new item(s) to be added to the array

Discussion (0)

pic
Editor guide