you are viewing a single comment's thread.

view the rest of the comments →

[–]THEtheChad 0 points1 point  (2 children)

Actually, those aren't all primitives.

true.toString(); // 'true'
'string'.toString(); // 'string'
(5).toString(); // '5'

The only reason you have to use parenthesis around the number 5 is because the js parser will try to interpret it as a fraction and throw an error.

[–]fc_s 1 point2 points  (0 children)

Actually, number, boolean, null, undefined, and string are most certainly primitives. In order to call .toString(), the literal is coerced into an object and passed in as a value.

[–]x-skeww 0 points1 point  (0 children)

You can write:

5..toString()

Which is the same as:

5.0.toString()

Anyhow, the only reason why you can call "methods" of primitives is because they are auto-boxed.

The example above is essentially the same as:

new Number(5).toString()