Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 3.toString() throw a syntax exception and 3..toString() works fine? [duplicate]

Tags:

javascript

why in javascript 3.toString() throws exception and 3..toString() works fine? I saw it in a funny presentation about javascript but I cannot find the info WHY. Thank you in advance.

like image 527
homar Avatar asked Dec 25 '22 12:12

homar


1 Answers

Because a decimal point is a valid portion of a number, so the first dot is considered numeric, the second is for chaining.

If you'd prefer to avoid the double-period you could instead do:

(3).toString();

Or:

'' + 3;

Or:

String(3);
like image 71
David Thomas Avatar answered Jan 05 '23 19:01

David Thomas