Javascript toString Function

See the code below:

2.toString(); // error 2..toString(); // "2" 2...toString(); // error 

I want to know why 2..toString() can work without errors and what happens when it starts?

Can someone explain this?

+6
source share
1 answer

http://shamansir.imtqy.com/JavaScript-Garden/en/#object

A common misconception is that numeric literals cannot be used as objects. This is because a flaw in the JavaScript parser is trying to parse dot notation on a number as a floating point literal.

 2.toString(); // raises SyntaxError 

There are several workarounds that can be used to create numeric literals as objects.

 2..toString(); // the second point is correctly recognized 2 .toString(); // note the space left to the dot (2).toString(); // 2 is evaluated first 
+8
source

Source: https://habr.com/ru/post/954555/


All Articles