3

Does anyone know the reason behind why JavaScript Arrays start with an index of 0 instead of 1? Or why the .length property starts at 1 instead of 0.

Comments
  • 1
    @EveryNameIsGone it doesn’t, I was just giving an example of something that doesn’t start at 0. You could say the same thing about the index of the array.

    arr.length is 2.
    But if I want to select that second item then you have to “arr[1]”.

    I was just curious to the logic behind it, as I’m sure there’s a reason for it.
  • 3
    length property starts at 0, when you have no elements inside. Length is one, when you have one element is inside.
  • 2
    @killermenpl thanks! I was mainly asking why arrays index from 0 instead of 1. I only used .length as an example of something that starts counting from 1 instead of 0.

    But thanks for a little insight.
  • 9
    What @killermenpl said. Arrays use an offset to find the data needed, the index. Let's say you have memory location 1337 for that array, that's exactly the location the first element is saved at, so you use an offset of 0 to access that element. That's translates into something like READ AT 1337+0. Now if you want the data at index 4, it becomes read at 1337+4 etc.
    Length tells you how many elements are used in that array. 1 Element at offset 0 is still 1 element. You get the clue :)
    Now when you want the 4th element of an array, you need to subtract one, since you always have the offset of one in relation to the memory location
  • 3
    @Kimmax thank you sirrr! And @killermenpl

    That makes total sense. Thanks for taking the time to explain the indexing.
  • 8
    0-indexed arrays originate from assembler code and pointer arithmetic.

    Consider you have an array of [123, 456, 789], each a 32 bit/4 byte integer, in lower level languages it would look like this in memory:

    [byte address - 32 bit value]
    20000 - 123
    20004 - 456
    20008 - 789

    To create such an array, you would request an adress of at least 12 free bytes (3x 32 bit) of memory from the memory manager. C example:

    int* myarray = malloc(sizeof(int) * 3);
    // let's assume myarray is now 20000

    Now, to set a value of an arbitrary index, we would need to set a value in memory at 20000 + (4 * index).
    In C, the index will get multiplied by the pointer type width (int* = 4 bytes wide), so we don't need to explicitly multiply:

    *(myarrray + index) = value;
    // or
    myarray[index] = value;

    To write the first element we would write at address 20000 or 20000 + (4 * 0), therefore the index for the first element is 0.

    Keep in mind that while the standard of 0-indexed arrays is widespread, there are still languages that start array indices at 1.

    edit: damn you, Kimmax! I type too slow 🤣
  • 1
    @hawkes thanks for the thorough explanation big fella!

    What’s funny is the passive aggressive grammar correction at the end.
  • 2
    @Nilo-jxn what passive-agressiveness? I might've missed it, english is not my first language. 🤔
  • 2
    @hawkes oh no worries; I said “indexes” instead of “indices”.

    But, I just realized both forms of the word are correct
  • 3
    The solution by @Kimmax is more correct anyway since he uses memory address 1337 😜
  • 2
    `for(i=0; i<array.length; i++)`
    Nice and clean.

    Likewise, `array_ptr + sizeof(int)*i` is easy math.

    Note to self: read comments first.
    Exhaustion is no excuse for duplication.
Add Comment