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

In JavaScript, it is permissible to put an integer numeric literal with a period, for example ...

x = 5.; 

What is the meaning of this designation? Is there a reason to put an end to the end, and if not, why is this designation allowed in the first place?

Update: Ok guys, as you mention float and integers ... We are talking about JavaScript here. JavaScript has only one type of number, which is IEEE-754.

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

+6
javascript numbers literals
source share
5 answers

You need a decimal point if you call a method for an integer:

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

If this does not look right, (5).toFixed(n) or 5.0.toFixed(n) will work too.

+2
source share

I guess this is just compatibility with other C-like languages, where the point matters.

+4
source share

The correct answer in this case is that it has absolutely no value.

Each number in JavaScript is already a 64-bit floating point number.

" . syntax " is only useful when you can omit the fixed part because it is 0 :

 .2 // Will end up as 0.2 -.5 // Will end up as -0.5 

Thus, it just saves bytes, but makes the code less readable at the same time.

+2
source share

This is a floating point number. Unlike any other language I've ever come across, all numbers in Javascript are actually 64-bit floating numbers. Technically, in Javascript there are no eigen integers. See Full Javascript Number Description for a complete ugly story.

+1
source share

What if it is not an integer, but a floating point character?

-one
source share

All Articles