According to ECMA Script 5.1 Technical Specifications , the grammar for decimal letters is defined as follows
DecimalLiteral :: DecimalIntegerLiteral . [DecimalDigits] [ExponentPart] . DecimalDigits [ExponentPart] DecimalIntegerLiteral [ExponentPart]
Note: The square brackets are intended only to indicate that parts are optional.
So when you say
3.toFixed()
After using 3. , the parser considers that the current token is part of the decimal literal, but only DecimalDigits or ExponentPart can follow it. But it finds t , which is invalid, so it does not work with SyntaxError.
when you do
3..toFixed()
After using 3. he sees . called the property access operator. Thus, it omits the optional DecimalDigits and ExponentPart and builds a floating-point object and proceeds to call the toFixed() method.
One way to overcome this is to leave a space after the number, for example
3 .toFixed()
source share