In JavaScript, if you set an array to be of size 5 ( var foo = new Array(5); ), is this just an initial size? Can you expand the number of elements after it is created. Is it possible to do something like this as well – arr = new Array() and then just assign elements one by one? Thanks in advance 🙂
In JavaScript, if you set an array to be of size 5 ( var
Share
Yes it is just an initial size, and it is not required. If you don’t use a single number, you can immediately populate.
It is also more common to use the simpler
[]syntax.You can set (or replace) a specific index by using brackets:
You can add to the end by using push:
There may be faster performance in some browsers if you do it like this instead:
You can add something at any index.
You can remove an item completely using splice: