DEV Community


Posted on • Updated 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)


11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!