Search code examples
javascriptarraysindexingstring-length

Deep understanding: What is the underlying logic behind the way array indexes work?


My motivation for this question

For me the thrill of programming is when a deep understanding of the logic behind a given language enables you to "just figure things out" using that logic. So when I stumble upon something that appears to miss this kind logic it bothers me.

How indexes work

This is what I have learned about working with array indexes:

var fruit = ["Apple", "Orange", "Kiwi"]

fruit.length = 3 // returns the length of the fruit array

var rotten = fruit[2] //assigns the value "Kiwi" to the variable "rotten"

fruit[2] = "Melon" //changes the 3rd object in the array fruit from "Kiwi" to "Melon"
fruit[fruit.length] = "Melon" //adds a new object to the array fruit after "Kiwi"

What bothers me about the logic

So I have just used .lengthto obtain the information that my fruit array has a length of three. Next I want to access the third object using var rotten = fruit[]. My logic tells me I should do this using var rotten = fruit[3]. But that returns undefined because the objects are indexed 0, 1, 2.

Likewise my immediate thought would be to use the following code to add an object to an array: fruit[fruit.length + 1] = "Melon" because I know that the .length method returns "the number of the last object" in the array. This is of course not actually the case because this in reality is 2 when considering the index is 0, 1, 2.

My question

Is there a deeper logic behind not indexing an array using 1, 2, 3 in stead of 0, 1, 2 and thereby making it - in my opinion - more intuitive to understand and use Javascript or is this just something you should learn my heart and accept without looking for any deeper reasoning?


Solution

  • In Fortran family languages, arrays are indexed starting at 1, as you suggest, since this is standard mathematical practice. In the Algol-60/PASCAL family of languages, the programmer chooses the starting index — it can be 0, 1, or anything else. In the BCPL and C family of languages, and in most modern languages (with the notable exclusion of Julia), array indices start at 0.

    Over the years, it has been found out that it is convenient to have a single convention — PASCAL-style indexing is confusing when code written by different programmers is combined, it is better to choose one starting index and stick to it.

    The actual choice between 0 and 1 is fairly arbitrary, but a number of wise people agree that starting at 0 makes a number of operations a little simpler and less error-prone. Compare for example the following two implementations of array interleaving, 0-based:

    if(i % 2 == 0)
        a[i / 2] = 42;
    else
        b[(i - 1) / 2] = 42;
    

    and 1-based:

    if(i % 2 == 1)
        a[(i + 1) / 2] = 42;
    else
        b[i / 2] = 42;