Why does 0.-5 evaluate to -5?

Trailing digits after a . are optional:

console.log(0. === 0); // true

So

0.-5

evalutes to

0 - 5

which is just -5. Similarly,

0.-5+1

is

0 - 5 + 1

which is

-5 + 1

or -4.


0.-5 could be successfully parsed as 0.[1], - and 5. Below is the abstract syntax tree for the expression generated by AST explorer:

Parse tree generated by AST explorer

This (in an unexpected way) is valid JavaScript and evaluates to -5.


[1] According to the grammar for numeric literals the decimal digits and exponent parts are optional:

NumericLiteral ::
  DecimalLiteral
  [...]

DecimalLiteral ::
  DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt