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.

Required fields*

12
  • Why would you want to use a negative index? That's very confusing. I don't see the point unless I'm missing something. Commented Nov 29, 2012 at 3:58
  • 17
    You can also write arr[1.5] = 1 and that also does not affect the length. The language specification is very clear about what affects the length. You may not like it but you have to live with it. Either that or design your own competing language and convince people to switch to it. Commented Nov 29, 2012 at 4:00
  • 2
    @DigvijayYadav: Could be considered a flaw or a feature like many other things in JavaScript. This is because arrays behave a bit like objects, you can also use a string var a = []; a['foo'] = 'baz' but that doesn't mean you should; it's clearly against all conventions. Commented Nov 29, 2012 at 4:03
  • 3
    Guys, the main point here is that ARRAYS ARE OBJECTS. There is no difference. That is the reason for this behavior, and it's completely intentional even if you don't like it. Just wait until you learn that ALL numbers are represented as floating point, even integers. JavaScript is a curious language... Commented Nov 29, 2012 at 4:16
  • 2
    You can find details of the algorithm that defines the setting of array elements in the specification: es5.github.com/#x15.4.5.1.(especially step 4) and "array index" is defined here: es5.github.com/#x15.4. Commented Nov 29, 2012 at 4:20