DEV Community

Sadick
Sadick

Posted on • Edited on

Mental Models: Array Indexing

Having arrays start at index zero is somewhat confusing at first. Naturally we start counting things from one. We see zero as representing nothing.

The confusion comes from thinking of the index as an ordinal number. An ordinal number defines the position of something in a series i.e. first, second, third e.t.c. This logically means we can't have an item occupying position zero in a series. If we think of the index as an ordinal number, the first element would be at e[1].

Your thinking should shift from thinking of index as an ordinal number. You should be thinking of the index as an offset from a starting point. From this thinking the first element would be a distance of zero from the beginning, hence e[0].

Top comments (0)