Why suppress zero before the decimal point, as in 0.5 == .5?

3

Sometimes I see a code in which the programmer has written a non-zero decimal, such as 0.5 being .5 , for example:

var decimal1 = 0.5;
var decimal2 = .5;

console.log(decimal1 == decimal2);
console.log(decimal1, decimal2);

In my point of view , writing a decimal as .5 leaves the reading impaired, so I always prefer to use it with zero: 0.5 .

What actually happens when we determine a decimal without zero before the point? Why can not we define a decimal variable like this? Are there differences between the two? Is there any increase in performance?

    
asked by anonymous 17.07.2018 / 15:15

1 answer

4
  

What actually happens when we determine a decimal without zero before the point?

Nothing, this is a syntax question, does not change the execution, the meaning, nothing, only one character less.

The number is the same, this is just a representation of it on the screen in this context. Note that writing on screen is also just a textual representation. It was always convenient to put 0 before the dot, which seems a bit incoherent.

JS is not known to have a strong requirement for strict syntax.

  

Why did we get to define a decimal variable like this?

The simple and obvious answer is that the language defined it. As there is no ambiguity they felt that it was not necessary to force something for readability.

One hypothesis is to keep the language less verbose, although this would be questionable. They even talked about getting a smaller file, but this happens so few times that I doubt it would be a good motivation. If that were the case, the language would have other things that allow the text to be smaller in a more intense way and with less readability problems.

The middle math that defines so, zeros in the integer to the left of the number have no meaning, and so can be omitted. Just like the zeros on the right in the decimal part.

  

Are there differences between the two ways?

Zero.

  

Is there any increase in performance?

Zero.

Do your best, because it has little relevance.

    
22.07.2018 / 23:14