4

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.

user2864740
  • 57,407
  • 13
  • 129
  • 202
homar
  • 541
  • 7
  • 18

1 Answers1

10

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);
katspaugh
  • 16,674
  • 11
  • 64
  • 100
David Thomas
  • 240,457
  • 50
  • 366
  • 401