Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 8
    \$\begingroup\$ note: I know this is obvious, but if we are going to insert elements too close to index 0, it might be good idea to write it like this a=[15,30]; \$\endgroup\$ Commented Dec 18, 2012 at 16:36
  • 7
    \$\begingroup\$ You can also leave elements undefined (say if you want a 2 indexed version of the above -> a=[,,15,30];. Also, I think you're suggesting adding things to the array using a[a.length]=x, but a.push(x) is shorter... \$\endgroup\$ Commented Aug 7, 2014 at 1:59
  • \$\begingroup\$ Not a[a.length]. @Alconja is correct in suggesting a.push. \$\endgroup\$ Commented Feb 12, 2015 at 4:42
  • \$\begingroup\$ a.push(4) // returns index a[a.length]=5 // returns new value a=[...a,3] // returns array [...a,3] // doesn't mutate \$\endgroup\$ Commented Jan 26, 2018 at 15:24
  • \$\begingroup\$ well it seems breaks and pre markdown isn't working :-( \$\endgroup\$ Commented Jan 26, 2018 at 15:26