0

In the JavaScript console, if I execute:

2.toString()

I get the error:

Uncaught SyntaxError: Invalid or unexpected token

But if I execute (2).toString(), the result is "2".

Why do I have to use the parentheses in order to apply methods from the Number prototype?

Other "primitives" work without parentheses. For example:

true.toString() => "true"

"".toString() => ""

It doesn't work for null or undefined, of course, since by definition they don't have any properties or methods.

Why is Number different from Boolean and String in this way?

Isaac Lyman
  • 2,175
  • 1
  • 22
  • 42

0 Answers0