Why do programmers count from zero?

Three numbered index cards, 0, 1 and 2, above a green baseline beside the words Why start at zero?

Ask a person for the first item in a list and they will point to item one. Ask a program for the first item in an array and you may have to write items[0]. It can look as though programmers have forgotten how to count.

They have not. First is an ordinal: it tells you an item’s place in a human sequence. An array index can answer a different question: how far is this item from the beginning? The first item is zero steps away.

That small change of viewpoint explains much of zero-based indexing. It also explains why the convention is useful without pretending that every language must adopt it.

An index as an offset

Imagine three cards in a row: red, green and blue. The red card has no cards before it, the green card has one, and the blue card has two. Those counts give them indexes 0, 1 and 2.

In C, this offset interpretation is especially literal. The expression a[i] means the element reached by moving i elements from the beginning of the array: it is equivalent to *(a + i). The GNU C manual describes this relationship directly. At i = 0, nothing is added to the starting address, so the first element is right there.

This is a useful explanation for C’s indexing model, not a claim that pointer arithmetic invented the convention or that every language implements arrays as C does. In JavaScript arrays, the same visible numbering applies even though programmers do not work with C pointers: the first element has index 0, and the last has index length - 1.

For an array of three cards, the valid indexes are 0, 1 and 2. Its length is still 3 because it contains three cards. Index 3 marks the position after the last card; it is not a fourth card you can read.

Why the end is left out

Zero-based indexes become even more helpful when a range includes its start but excludes its end. Such a range is written mathematically as [start, end). The cards at indexes 0, 1 and 2 occupy the range [0, 3). Its size is simply 3 - 0 = 3.

The same rule works for a smaller piece. In JavaScript, items.slice(1, 3) returns the elements at indexes 1 and 2, not the element at 3. The documentation for slice defines its end as exclusive. The result has 3 - 1 = 2 elements.

Half-open ranges also fit together without an overlap or a gap. The range [0, 1) contains the first card; [1, 3) contains the next two. Both pieces meet at boundary 1. Python’s tutorial uses this boundary model to explain slices: the left edge is included, the right edge is excluded, and the difference between them gives the length.

This is why a familiar loop often reads i = 0; i < length; i++. It starts at the first valid offset and stops before the one-past-the-end boundary. The loop condition and the array’s length describe the same endpoint.

Dijkstra’s argument was not the origin story

In 1982, Edsger Dijkstra wrote a short note called “Why numbering should start at zero”. He argued that a number can represent how many elements precede a position. Starting at zero then makes a sequence of N elements run from 0 up to, but not including, N.

Dijkstra was defending a convention already in use, not announcing its invention. His note is valuable because it makes the arithmetic behind the choice explicit. A range’s upper bound minus its lower bound tells you how many elements it contains. The zero at the beginning is doing work.

It is a convention, not a law

Other choices work. MATLAB, for example, uses one-based array indexes: its array-indexing guide addresses the first row or column with 1. For a person reading a table, that can feel more natural. Dijkstra’s note itself discusses languages whose indexing starts at one.

No computer needs to believe that zero is the “real” first number. The best indexing rule depends on the language and the interface. What matters when writing code is knowing which meaning a particular index carries. An ordinal answers which item is this? An offset answers how far from the start?

So programmers do not count three objects as zero, one and two. They count three objects as three. They often label their positions by the distance from the beginning: zero, one and two. Once you see the label as a distance, the first zero stops looking like a mistake.

Wear it

  • The word Babel hand-lettered in yellow brush script with a black outline

    Babel Logo

    t-shirt · sticker

    BABEL hand-lettered in yellow marker, deliberately rough. A compiler logo that looks like it was scrawled on a whiteboard, which suits a tool most people never think about.

  • The Flow type checker logo, an angular letter F in overlapping yellow and orange facets

    Flow Logo

    t-shirt · sticker

    Flow's angular F in overlapping yellows and oranges. Meta's other type checker, comprehensively beaten by TypeScript, and increasingly a collector's item.

  • The JavaScript logo, black JS letters on a bright yellow square

    JavaScript Logo

    t-shirt · sticker

    The yellow square with two black letters. No foundation designed it and no one owns it — the community adopted a logo somebody posted on GitHub and never looked back.

  • The Jest logo, a dark red jester hat with three bells above the word Jest

    Jest Logo

    t-shirt · sticker

    The dark red jester's hat, bells and all. Testing framework branding rarely bothers with a character; Jest committed to the pun and drew the whole hat.

  • The TypeScript logo, white TS letters on a blue square

    TypeScript Logo

    sticker

    The blue TS square. Somewhere between 2015 and now this stopped being a preference and became the default, and the logo went from niche to unavoidable with it.

  • The Webpack logo, a pale blue isometric cube beside the word webpack in black

    Webpack Logo

    t-shirt · sticker

    The isometric blue cube with the wordmark alongside. A genuinely elegant mark for a tool whose configuration file has reduced grown adults to tears.

Read next