What really happens when we determine a decimal without zero before the point?
Nothing, this is a matter of syntax, does not change the execution, the meaning, nothing, has only one character less.
The number is the same, that’s just a representation of it on the screen in this context. Note that the writing on the screen is also just a textual representation. It was always agreed to put 0 before the point, which seems a little inconsistent.
JS is not known to have a strong strict syntax requirement.
Why can we define a decimal variable this way?
The simple and obvious answer is that language defined it like this. As there is no ambiguity they felt that it was not necessary to force something just for readability.
One hypothesis is to keep the language less verbose, although that would be questionable. They even talked about getting a smaller file, but it happens so few times that I doubt it would be a good motivation. If this were the case the language would have other things that allow the smaller text more intensely and with less readability problems.
Mathematics sort of defines zeros in the left integer of the number as meaningless, and so can be omitted. As well as the zeros on the right in the decimal part.
There are differences between the two ways?
Zero.
Is there any increase in performance?
Zero.
Do as you see fit, because it has little relevance.
Can anyone tell me why
-1
?– bio
There is a technique, the fewer letters on a page, the less weight. But I don’t know if writing like this is worth it. Notice what javascript optimizers/reducers do with the page, some arrive "eat" part of code that I would be ashamed to write.
– Metamorfose
Not having "0" before does not negatively affect readability (in the general case). It is a widely accepted convention. Whether or not it turns out to be just a stylistic and cultural issue.
– Jefferson Quesado
@Jeffersonquesado, yes. I made it clear that my point of view, is that why the negative votes? But why is there this stylistic question and why does language allow it? The
.x
exists for some reason?– bio
You can even test on a homemade calculator, if you type . 5, it will understand that it is 0.5. I think it’s only by convention even, in college, I had a numerical calculus course, where I had a topic that taught the best rounding practices, where we used very tricky numbers, like 0.0089240544.... Ai at the time of writing on the calculator, the teacher himself recommended this form . 0089240544 because whether you like it or not, it becomes easier for us to write, and at the time of reading on the calculator display, it seems funny, but one less character ended up making it easier, and a lot!
– MarceloBoni
@Marceloboni, cool. I’ve noticed that in other languages this happens too. The answer to this is beyond programming, then? I was even more curious... I had a teacher who said otherwise, even though we weren’t going to do a calculator bill, said to keep zero so we wouldn’t get lost in the bills.
– bio
I think it goes into the same case of numbers defined as
0x, 0b e 0o
vine. The javascript interpreter uses some regex to detect what comes after the0x
,0b
,0o
and.
to make the conversion calculation... note that if you write only.
no number after, the console sends a syntax error.– MarceloBoni
@bio is standard that the recognition of floating point numbers is provided through something close to the expression
([1-9][0-9]*|0?)(\.[0-9]*)?
– Jefferson Quesado
What worries me is that it works:
console.log(parseFloat(".2fhh.95fgh/4")); // 0.2
for example– MarceloBoni