Why does 0.-5 evaluate to -5?

  • A+
Category:Languages

Suppose I write 0.5 as 0.-5 in unexpected way, but it can still run. What does 0. in 0.-5 do so that it can still run and evaluates to -5?

I also tried alert(0.-5+1) which prints -4, does JavaScript ignore 0. in 0.-5?

 


Trailing digits after a . are optional:

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

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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: