Reputation: 105
Which one?
arr[arr.length+1] = value
arr[arr.length] = value
arr[arr.length-1] = value
arr = arr + value
Upvotes: 0
Views: 2188
Reputation: 26191
Depends on what you want to receive on return.
array[array.length] = thingy // <- thingy
array.push(thingy) // <- array.length
array.concat(thingy)
(care for array items to be added since they get spread i.e. [].concat([1,2,3],[4,5,6]); // <- [1, 2, 3, 4, 5, 6]
)Upvotes: 0
Reputation: 1
You can use the second option.
arr[arr.length] = value
Array
instance .length
is 0-based, beginning at 0
. An array having three elements [1,2,3]
will have .length
3
, though indexes 0
through 2
will be populated with values 1
through 3
. arr[arr.length] = value
sets value
at index 3
, where resulting .length
will be 4
.
Upvotes: 0
Reputation: 5272
simply use
arr.push(value);
the value will be added to the end of the array
Upvotes: 3