Reputation: 9788
I'm curious, I have programmed JavaScript already few years but sometimes I get confused when I see the following variable declarations: (ofc. those could be any other numbers as well).
var exampleOne = 0.5;
var exampleTwo = .5;
What is the difference between these two, or is there any? Are there some sort of hidden benefits which I clearly don't understand?
Upvotes: 3
Views: 3001
Reputation: 61935
There is no difference.
The Numeric Literals are parsed equivalently - that is, both 0.5
and .5
(as would .50
) represent the same number. (Unlike most other languages, JavaScript has only one kind of number.)
I prefer to always include the [optional] leading 0 before the decimal.
Upvotes: 2
Reputation: 816790
To quote the specification:
0.5
matches the rule DecimalLiteral :: DecimalIntegerLiteral . DecimalDigits which is evaluated as (MV means mathematical value):
The MV of DecimalLiteral :: DecimalIntegerLiteral . DecimalDigits is the MV of DecimalIntegerLiteral plus (the MV of DecimalDigits times 10–n), where n is the number of characters in DecimalDigits.
.5
matches the rule DecimalLiteral :: . DecimalDigits which is evaluated as
The MV of DecimalLiteral :: . DecimalDigits is the MV of DecimalDigits times 10–n, where n is the number of characters in DecimalDigits.
So you can see that the only difference is that the value of the digits preceding the .
are added to the final value. And adding 0
to a value doesn't change the value.
Upvotes: 4