Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an integer literal followed by a dot a valid numeric literal in JavaScript?

In JavaScript it is valid to end an integer numeric literal with a dot, like so...

x = 5.;

What's the point of having this notation? Is there any reason to put the dot at the end, and if not, why is that notation allowed in the first place?

Update: Ok guys, since you mention floats and integers... We are talking about JavaScript here. There is only one number type in JavaScript which is IEEE-754.

5 and 5. have the same value, there is no difference between those two values.

like image 251
Šime Vidas Avatar asked Sep 12 '10 17:09

Šime Vidas


2 Answers

I guess it is just compatibility with other C-like languages where the dot does matter.

like image 161
kennytm Avatar answered Oct 28 '22 20:10

kennytm


You DO need the decimal point if you call a method on an integer:

5.toFixed(n) // throws an error

5..toFixed(n) // returns the string '5.' followed by n zeroes

If that doesn't look right, (5).toFixed(n), or 5.0.toFixed(n), will work, too.

like image 25
kennebec Avatar answered Oct 28 '22 19:10

kennebec