Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7714741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:12:25+00:00 2026-06-01T02:12:25+00:00

In the console of both FF and Chrome, {} is considered undefined until explicitly

  • 0

In the console of both FF and Chrome, {} is considered undefined until explicitly evaluated:

{};     // undefined
({});   // ▶ Object

Actually, it’s a bit less defined than undefined — it’s apparently bad syntax:

{} === undefined;  // SyntaxError: Unexpected token ===
{}.constructor;    // SyntaxError: Unexpected token .

But not if it’s on the other side, in which case it’s fine:

"[object Object]" == {}.toString(); // true

Or if it’s not the first expression:

undefined + undefined; // NaN
{} + undefined;        // NaN
undefined + {};        // "undefined[object Object]"

What gives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:12:27+00:00Added an answer on June 1, 2026 at 2:12 am

    Okay, here is my answer. There is nothing new here. I am just linking to (a pretty copy of) the ECMAScript specification for the grammar and showing a few productions to show “why” it parses the way it does. In any case, the behavior is well-defined according to the JavaScript/ECMAScript grammar rules: {} is parsed differently depending upon the “context” it is in.


    The JavaScript REPLs (“consoles”) start to parse the code in the Statement grammar production or “statement context”. (This is actually a lie, it starts at the Program or SourceElements production, but that adds additional constructs to dig through.) Here is a rough grammar breakdown with simplifications and omissions; see the link above for more:

    Statement
        Block
        ...
        ExpressionStatement
    
    Block
        # This is actually { StatementList[optional] }, but this is what
        # it amounts to: * means "0 or more".
        { Statement* }
    
    ExpressionStatement
        # An ExpressionStatement can't start with "{" or "function" as
        # "{" starts a Block and "function" starts a FunctionStatement.
        [lookahead ∉ {{, function}]Expression ;
    
    Expression
        # This is really PrimaryExpression; I skipped a few steps.
        ...
        ( Expression )
    

    Thus (when in “statement context”):

       {}
    -> Block  # with no StatementList (or "0 statements")
    -> Statement
    

    And:

       ({})
    -> (Expression)
    -> Expression
    -> ExpressionStatement  # omitted in productions below
    -> Statement
    

    This also explains why undefined === {} parses as EXPR === EXPR -> EXPR -> STMT and results in false when evaluated. The {} in this case is in an “expression context”.

    In the case of {} === undefined it is parsed as {}; === undefined, or BLOCK; BOGUS -> STMT; BOGUS, which is a Syntax Error. However, with the addition of parenthesis this changes: ({} === undefined) is parsed as (EXPR === EXPR) -> (EXPR) -> EXPR -> STMT.

    In the case of {} + "hi" it is parsed as {}; + "hi", or BLOCK; + EXPR -> STMT; EXPR -> STMT; STMT, which is valid syntax even though it is silly (+ is unary in this case). Likewise, just as above, "hi" + {} puts the {} into an “expression context” and it is parsed as EXPR + EXPR -> EXPR -> STMT.

    The JavaScript console is just showing the result of the last Statement, which is “undefined” (well, “nothing” really, but that doesn’t exist) for an empty {} block. (This might vary between browsers/environments as to what is returned in this case, e.g. last ExpressionStatement only?)

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a console application that communicates with a web service. Both of them
I have a solution that contains both a Windows Forms application and a Console
What's the difference between console.log(process.cwd()) and console.log(__dirname); I've seen both used in similar contexts.
I have 2 files, A.cpp and B.cpp, in a Win32 console application. Both 2
in firefox I can usee firebug, in chrome I can use the css console.
I have the following code, and the javascript console in chrome says Can't read
I've just started using the console in Chrome Developer Tools (pretty new to Javascript
I do a lot of my work from Chrome's developer tools console and firebug
Console dogs me. Can anybody help? The sad story starts like this. My OS
direct console to ok.log, and errors to error.log for example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.