1

So I have this:

5.toString()

I get this error:

console.log(5.toString());
            ^^

SyntaxError: Invalid or unexpected token
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)

However, if I pass 5 using an intermediary function, it seems to cause no problems:

const foo = function(v){
  console.log(v.toString());
};


foo(5);   // no problem now

does anyone know why that runs without an error? I would think it would cause an exception. Maybe it's just a "compile-time" exception, not a runtime exception.

1 Answers1

0

Put it in parens like this:

(5).toString()

You can't use it the other way because the lexer is thinking you're typing out a decimal number

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33