Skip to content

Instantly share code, notes, and snippets.

@mvsde
Last active February 5, 2023 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvsde/8e3bad36f0db5dac5c365d234962f17e to your computer and use it in GitHub Desktop.
Save mvsde/8e3bad36f0db5dac5c365d234962f17e to your computer and use it in GitHub Desktop.
Why is `Number("") === 0`?

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:

JavaScript 1.1 empty string to number conversion would’ve thrown an error

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment