Hacker News new | ask | show | jobs
by _v7gu 1992 days ago
Just using parenthesis is enough for wrapping:

    > 0.toString()
    Thrown:
    0.toString()
    ^^
    
    SyntaxError: Invalid or unexpected token
    > (0).toString()
    '0'
1 comments

Parens aren't doing the same thing. Primitives are implicitly being wrapped in objects when you use methods, but they come out on the other end as primitives again. The new operator gives you an object wrapper that you can keep working with and modifying the values of, whereas the parens you're using just let the interpreter know that your period isn't a decimal point. You can also use a space for this.

  > typeof(new Number(0));
  'object'
  > typeof((0));
  'number'
  > 0 .toString();
  '0'