Skip to content

The Number("") === 0 conundrum

The case

This video by ThePrimeagen sent me on a fun archaeology trip.

The case to solve: Why is the following statement true? Why is an empty string parsed as a number in JS?

Number("") === 0;

ECMAScript 0.14 and 1 (1997)

ECMAScript 0.14 from March 1997 and later ECMAScript 1 from June 1997 contain the following on page 26 and 28 respectively:

The MV of StringNumericLiteral ::: (an empty character sequence) is 0.

0.14 is the first version of the spec where this appears.

JavaScript 1.1 (1996)

But JavaScript 1.1 from November 1996 states this on page 23:

To type
Function Object Number Boolean String
From type undefined error null error false "undefined"
Function N/C Function object valueOf/error valueOf/true decompile
Object not null Function object N/C valueOf/error valueOf/true toString/valueOf
null error 0 false "null"
Number zero error Number N/C false "0"
nonzero true default
NaN false "NaN"
+Infinity true "+Infinity"
-Infinity true "-Infinity"
Boolean false error Boolean 0 N/C "false"
true 1 "true"
String empty error String error false N/C
non-empty number/error true

So, something happened between 1996 and 1997. But I couldn’t figure out what. There is an archive of TC39 documents from 1997, though nothing hints at the reasoning for this decision.

JScript 0.1 (1996)

JScript 0.1 from 1996 seems to be the source for something similar:

Number() === 0;

On page 14 it states:

The Number constructor has two modes.

  • With no arguments, it returns an object whose valueOf method returns 0.
  • With one argument, which must be a number, it returns an object whose valueOf method returns the argument.

Fun side tangent

Alternative names for ECMAScript were collected, some are actually pretty good but others are totally wild:

Name Explanation
CoolScript No known issues
CoScript No known issues. Stands for "Common Scripting System"
Descartes Unknown -- too many matches.
DeScript Unknown -- too many matches. In honor of Descartes.
DynaScript Used in a physics class at Moorhead State
EScript No known issues
InfoScript No known issues
JScript None, but it is in use by Microsoft
JustScript No known issues
JSL Acronym used -- Japanese as a Second Language. For "Just a Script Language"
RadScript No known issues
ScriptJ No known issues
TranScript Unknown -- too many matches.
W3Script No known issues
WScript No known issues
wwwscript No known issues