{}.hasOwnProperty('id'); returns syntax error in console












2















I played around in the Chrome console and wondered why this statement throws a syntax error:



{}.hasOwnProperty('id');


My expectation was the return value false.

A syntax error also occurs in Firefox and IE .

The same works if I use an empty array instead of an empty object.

The same also works if I put braces around:



({}.hasOwnProperty('id'));









share|improve this question

























  • FYI: ({}).hasOwnProperty('id'); works, too.

    – Cerbrus
    Oct 9 '14 at 11:55








  • 6





    {} without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.

    – Frédéric Hamidi
    Oct 9 '14 at 11:56






  • 2





    In this case, {} will be interpreted as a block statement rather than an object literal.

    – Ben Jackson
    Oct 9 '14 at 11:57











  • Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?

    – enguerranws
    Oct 9 '14 at 11:58
















2















I played around in the Chrome console and wondered why this statement throws a syntax error:



{}.hasOwnProperty('id');


My expectation was the return value false.

A syntax error also occurs in Firefox and IE .

The same works if I use an empty array instead of an empty object.

The same also works if I put braces around:



({}.hasOwnProperty('id'));









share|improve this question

























  • FYI: ({}).hasOwnProperty('id'); works, too.

    – Cerbrus
    Oct 9 '14 at 11:55








  • 6





    {} without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.

    – Frédéric Hamidi
    Oct 9 '14 at 11:56






  • 2





    In this case, {} will be interpreted as a block statement rather than an object literal.

    – Ben Jackson
    Oct 9 '14 at 11:57











  • Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?

    – enguerranws
    Oct 9 '14 at 11:58














2












2








2








I played around in the Chrome console and wondered why this statement throws a syntax error:



{}.hasOwnProperty('id');


My expectation was the return value false.

A syntax error also occurs in Firefox and IE .

The same works if I use an empty array instead of an empty object.

The same also works if I put braces around:



({}.hasOwnProperty('id'));









share|improve this question
















I played around in the Chrome console and wondered why this statement throws a syntax error:



{}.hasOwnProperty('id');


My expectation was the return value false.

A syntax error also occurs in Firefox and IE .

The same works if I use an empty array instead of an empty object.

The same also works if I put braces around:



({}.hasOwnProperty('id'));






javascript google-chrome






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 9 '14 at 11:55









Cerbrus

50k1096117




50k1096117










asked Oct 9 '14 at 11:52









user3745438user3745438

134




134













  • FYI: ({}).hasOwnProperty('id'); works, too.

    – Cerbrus
    Oct 9 '14 at 11:55








  • 6





    {} without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.

    – Frédéric Hamidi
    Oct 9 '14 at 11:56






  • 2





    In this case, {} will be interpreted as a block statement rather than an object literal.

    – Ben Jackson
    Oct 9 '14 at 11:57











  • Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?

    – enguerranws
    Oct 9 '14 at 11:58



















  • FYI: ({}).hasOwnProperty('id'); works, too.

    – Cerbrus
    Oct 9 '14 at 11:55








  • 6





    {} without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.

    – Frédéric Hamidi
    Oct 9 '14 at 11:56






  • 2





    In this case, {} will be interpreted as a block statement rather than an object literal.

    – Ben Jackson
    Oct 9 '14 at 11:57











  • Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?

    – enguerranws
    Oct 9 '14 at 11:58

















FYI: ({}).hasOwnProperty('id'); works, too.

– Cerbrus
Oct 9 '14 at 11:55







FYI: ({}).hasOwnProperty('id'); works, too.

– Cerbrus
Oct 9 '14 at 11:55






6




6





{} without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.

– Frédéric Hamidi
Oct 9 '14 at 11:56





{} without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.

– Frédéric Hamidi
Oct 9 '14 at 11:56




2




2





In this case, {} will be interpreted as a block statement rather than an object literal.

– Ben Jackson
Oct 9 '14 at 11:57





In this case, {} will be interpreted as a block statement rather than an object literal.

– Ben Jackson
Oct 9 '14 at 11:57













Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?

– enguerranws
Oct 9 '14 at 11:58





Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?

– enguerranws
Oct 9 '14 at 11:58












2 Answers
2






active

oldest

votes


















4














There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:



if (x) {
...
}


and they are used to declare object literals:



var obj = {
prop: "value"
}


Constext is used to differentiate between the two interpretations, and in case of:



{}.hasOwnProperty('id');


braces are resolved to block declaration, so this yields syntax error. On the other hand:



({}.hasOwnProperty('id'));


One cannot declare block inside parentheses, so {} is recognized as object literal in this context.






share|improve this answer


























  • This is a complete answer IMHO.

    – enguerranws
    Oct 9 '14 at 12:04



















0














{} is treated as a statement block rather than an object, as you are expecting. Hence the error.



EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26277767%2fhasownpropertyid-returns-syntax-error-in-console%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:



    if (x) {
    ...
    }


    and they are used to declare object literals:



    var obj = {
    prop: "value"
    }


    Constext is used to differentiate between the two interpretations, and in case of:



    {}.hasOwnProperty('id');


    braces are resolved to block declaration, so this yields syntax error. On the other hand:



    ({}.hasOwnProperty('id'));


    One cannot declare block inside parentheses, so {} is recognized as object literal in this context.






    share|improve this answer


























    • This is a complete answer IMHO.

      – enguerranws
      Oct 9 '14 at 12:04
















    4














    There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:



    if (x) {
    ...
    }


    and they are used to declare object literals:



    var obj = {
    prop: "value"
    }


    Constext is used to differentiate between the two interpretations, and in case of:



    {}.hasOwnProperty('id');


    braces are resolved to block declaration, so this yields syntax error. On the other hand:



    ({}.hasOwnProperty('id'));


    One cannot declare block inside parentheses, so {} is recognized as object literal in this context.






    share|improve this answer


























    • This is a complete answer IMHO.

      – enguerranws
      Oct 9 '14 at 12:04














    4












    4








    4







    There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:



    if (x) {
    ...
    }


    and they are used to declare object literals:



    var obj = {
    prop: "value"
    }


    Constext is used to differentiate between the two interpretations, and in case of:



    {}.hasOwnProperty('id');


    braces are resolved to block declaration, so this yields syntax error. On the other hand:



    ({}.hasOwnProperty('id'));


    One cannot declare block inside parentheses, so {} is recognized as object literal in this context.






    share|improve this answer















    There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:



    if (x) {
    ...
    }


    and they are used to declare object literals:



    var obj = {
    prop: "value"
    }


    Constext is used to differentiate between the two interpretations, and in case of:



    {}.hasOwnProperty('id');


    braces are resolved to block declaration, so this yields syntax error. On the other hand:



    ({}.hasOwnProperty('id'));


    One cannot declare block inside parentheses, so {} is recognized as object literal in this context.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 9 '14 at 12:02









    enguerranws

    5,57543472




    5,57543472










    answered Oct 9 '14 at 12:00









    el.pescadoel.pescado

    16k23073




    16k23073













    • This is a complete answer IMHO.

      – enguerranws
      Oct 9 '14 at 12:04



















    • This is a complete answer IMHO.

      – enguerranws
      Oct 9 '14 at 12:04

















    This is a complete answer IMHO.

    – enguerranws
    Oct 9 '14 at 12:04





    This is a complete answer IMHO.

    – enguerranws
    Oct 9 '14 at 12:04













    0














    {} is treated as a statement block rather than an object, as you are expecting. Hence the error.



    EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.






    share|improve this answer




























      0














      {} is treated as a statement block rather than an object, as you are expecting. Hence the error.



      EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.






      share|improve this answer


























        0












        0








        0







        {} is treated as a statement block rather than an object, as you are expecting. Hence the error.



        EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.






        share|improve this answer













        {} is treated as a statement block rather than an object, as you are expecting. Hence the error.



        EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 9 '14 at 12:00









        gopi1410gopi1410

        4,15073368




        4,15073368






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26277767%2fhasownpropertyid-returns-syntax-error-in-console%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]