Search code examples
javascriptobjectconstructornumbersprototype

Difference between 1.constructor and (1).constructor


Why does (1).constructor return ƒ Number() { [native code] }, whereas 1.constructor returns Uncaught SyntaxError: Invalid or unexpected token?

What is the difference between the two and what exactly happens under the hood?


Solution

  • Your digit is part of a number literal. The first . in a numeric literal is the floating point and not the start of a property reference.

    You can see this in the spec:

    DecimalIntegerLiteral . DecimalDigits opt ExponentPart opt
    

    By wrapping the numeric literal in parentheses, you separate it from the . so the . is not treated as part of the numeric literal (and so can be used to read a property of the resulting object).