Question about priority of logical operatorsWhich equals operator (== vs ===) should be used in JavaScript comparisons?Is there a “null coalescing” operator in JavaScript?What is the !! (not not) operator in JavaScript?typeof !== “undefined” vs. != nullHow do I check if string contains substring?parseInt(null, 24) === 23… wait, what?Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?Logical operator in a handlebars.js #if conditionalWhy use Redux over Facebook Flux?Why does OR operator have so high priority

Is random forest for regression a 'true' regression?

How did the horses get to space?

Why did the soldiers of the North disobey Jon?

I recently started my machine learning PhD and I have absolutely no idea what I'm doing

Assembly writer vs compiler

Will consteval functions allow template parameters dependent on function arguments?

Is there any good reason to write "it is easy to see"?

How to handle professionally if colleagues has referred his relative and asking to take easy while taking interview

How will the lack of ground stations affect navigation?

Why did Varys remove his rings?

What was Varys trying to do at the beginning of S08E05?

What metal is most suitable for a ladder submerged in an underground water tank?

Getting a similar picture (colours) on Manual Mode while using similar Auto Mode settings (T6 and 40D)

Why are lawsuits between the President and Congress not automatically sent to the Supreme Court

Developers demotivated due to working on same project for more than 2 years

How to describe a building set which is like LEGO without using the "LEGO" word?

Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?

Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?

Were any of the books mentioned in this scene from the movie Hackers real?

Could a space colony 1g from the sun work?

What is the status of the Lannisters after Season 8 Episode 5, "The Bells"?

Does the Rogue's Reliable Talent feature work for thieves' tools, since the rogue is proficient in them?

tikz drawing rectangle discretized with triangle lattices and its centroids

Understanding Python syntax in lists vs series



Question about priority of logical operators


Which equals operator (== vs ===) should be used in JavaScript comparisons?Is there a “null coalescing” operator in JavaScript?What is the !! (not not) operator in JavaScript?typeof !== “undefined” vs. != nullHow do I check if string contains substring?parseInt(null, 24) === 23… wait, what?Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?Logical operator in a handlebars.js #if conditionalWhy use Redux over Facebook Flux?Why does OR operator have so high priority






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















The following code displays 3. Why? could you explain why?



alert( null || 2 && 3 || 4 );


Is there higher priority of && over || operator? why it doesn't display 2?










share|improve this question

















  • 1





    && does have higher precedence, but that doesn't matter here; it would evaluate to 3 under any precedence rules. Can you explain how you think it could evaluate to 2?

    – Paulpro
    Mar 23 at 15:11











  • null is false and 2 is true

    – user10214830
    Mar 23 at 16:20






  • 1





    @Paulpro is right. && evaluates to the right operand, never to the 2 on its left side. 2 && 3 and 2 && (3 || 4) are 3. Only null || 2 alone would evaluate to 2.

    – Bergi
    Mar 23 at 18:03

















1















The following code displays 3. Why? could you explain why?



alert( null || 2 && 3 || 4 );


Is there higher priority of && over || operator? why it doesn't display 2?










share|improve this question

















  • 1





    && does have higher precedence, but that doesn't matter here; it would evaluate to 3 under any precedence rules. Can you explain how you think it could evaluate to 2?

    – Paulpro
    Mar 23 at 15:11











  • null is false and 2 is true

    – user10214830
    Mar 23 at 16:20






  • 1





    @Paulpro is right. && evaluates to the right operand, never to the 2 on its left side. 2 && 3 and 2 && (3 || 4) are 3. Only null || 2 alone would evaluate to 2.

    – Bergi
    Mar 23 at 18:03













1












1








1








The following code displays 3. Why? could you explain why?



alert( null || 2 && 3 || 4 );


Is there higher priority of && over || operator? why it doesn't display 2?










share|improve this question














The following code displays 3. Why? could you explain why?



alert( null || 2 && 3 || 4 );


Is there higher priority of && over || operator? why it doesn't display 2?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 14:34







user10214830














  • 1





    && does have higher precedence, but that doesn't matter here; it would evaluate to 3 under any precedence rules. Can you explain how you think it could evaluate to 2?

    – Paulpro
    Mar 23 at 15:11











  • null is false and 2 is true

    – user10214830
    Mar 23 at 16:20






  • 1





    @Paulpro is right. && evaluates to the right operand, never to the 2 on its left side. 2 && 3 and 2 && (3 || 4) are 3. Only null || 2 alone would evaluate to 2.

    – Bergi
    Mar 23 at 18:03












  • 1





    && does have higher precedence, but that doesn't matter here; it would evaluate to 3 under any precedence rules. Can you explain how you think it could evaluate to 2?

    – Paulpro
    Mar 23 at 15:11











  • null is false and 2 is true

    – user10214830
    Mar 23 at 16:20






  • 1





    @Paulpro is right. && evaluates to the right operand, never to the 2 on its left side. 2 && 3 and 2 && (3 || 4) are 3. Only null || 2 alone would evaluate to 2.

    – Bergi
    Mar 23 at 18:03







1




1





&& does have higher precedence, but that doesn't matter here; it would evaluate to 3 under any precedence rules. Can you explain how you think it could evaluate to 2?

– Paulpro
Mar 23 at 15:11





&& does have higher precedence, but that doesn't matter here; it would evaluate to 3 under any precedence rules. Can you explain how you think it could evaluate to 2?

– Paulpro
Mar 23 at 15:11













null is false and 2 is true

– user10214830
Mar 23 at 16:20





null is false and 2 is true

– user10214830
Mar 23 at 16:20




1




1





@Paulpro is right. && evaluates to the right operand, never to the 2 on its left side. 2 && 3 and 2 && (3 || 4) are 3. Only null || 2 alone would evaluate to 2.

– Bergi
Mar 23 at 18:03





@Paulpro is right. && evaluates to the right operand, never to the 2 on its left side. 2 && 3 and 2 && (3 || 4) are 3. Only null || 2 alone would evaluate to 2.

– Bergi
Mar 23 at 18:03












4 Answers
4






active

oldest

votes


















0














&& operator precedence is higher than that of ||. Thus null || 2 && 3 || 4 expression is effectively the same as null || (2 && 3) || 4.



Let's evaluate the entire expression step by step:




  1. null is a falsy value, so check the next operand


  2. (2 && 3) results in 3 which is a truthy value, it stops here and returns 3





share|improve this answer
































    1















    Is there higher priority of && over || operator?




    Yes. This is called operator precedence. You can find a table for JS operators at MDN.



    Your expression is evaluated like



    alert((null || (2 && 3)) || 4);


    To get left-to-right evaluation, you will need to write



    alert((null || 2) && 3 || 4 );





    share|improve this answer




















    • 2





      It still logs 3 and even if the precedence of || and && were swapped it would still log 3.

      – Paulpro
      Mar 23 at 15:07


















    1














    The order of evaluation is



    null || (2 && 3) || 4
    null || 3 || 4
    3 || 4
    3


    logical AND && has a higher operator precedence than logical OR ||.






    console.log(null || 2 && 3 || 4); // 3








    share|improve this answer




















    • 1





      Order of evaluation doesn't matter much here. It would log 3 no matter what order: (null||2)&&(3||4) or (null||2) && 3 || 4 all evaluate to 3

      – Paulpro
      Mar 23 at 15:09


















    0














    The || operator runs next operand if it returns falsy value.



    The && operator runs next operand if it returns truthy value.



    Otherwise stop going further.



    So,



    - null || // false, go further
    - 2 && // true, go further
    - 3 || // true, stop


    Thus, finally returns 3.



    Edit:



    If you talk about priority of the operator, then you may look at the operator precedence. Where you can see && operator has precedence value with 6 and || operator has precedence value with 5. So, && operator has higher order.






    share|improve this answer

























    • Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.

      – Bergi
      Mar 23 at 18:00











    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%2f55314785%2fquestion-about-priority-of-logical-operators%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown
























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    && operator precedence is higher than that of ||. Thus null || 2 && 3 || 4 expression is effectively the same as null || (2 && 3) || 4.



    Let's evaluate the entire expression step by step:




    1. null is a falsy value, so check the next operand


    2. (2 && 3) results in 3 which is a truthy value, it stops here and returns 3





    share|improve this answer





























      0














      && operator precedence is higher than that of ||. Thus null || 2 && 3 || 4 expression is effectively the same as null || (2 && 3) || 4.



      Let's evaluate the entire expression step by step:




      1. null is a falsy value, so check the next operand


      2. (2 && 3) results in 3 which is a truthy value, it stops here and returns 3





      share|improve this answer



























        0












        0








        0







        && operator precedence is higher than that of ||. Thus null || 2 && 3 || 4 expression is effectively the same as null || (2 && 3) || 4.



        Let's evaluate the entire expression step by step:




        1. null is a falsy value, so check the next operand


        2. (2 && 3) results in 3 which is a truthy value, it stops here and returns 3





        share|improve this answer















        && operator precedence is higher than that of ||. Thus null || 2 && 3 || 4 expression is effectively the same as null || (2 && 3) || 4.



        Let's evaluate the entire expression step by step:




        1. null is a falsy value, so check the next operand


        2. (2 && 3) results in 3 which is a truthy value, it stops here and returns 3






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 14:48

























        answered Mar 23 at 14:42









        abadalyanabadalyan

        845613




        845613























            1















            Is there higher priority of && over || operator?




            Yes. This is called operator precedence. You can find a table for JS operators at MDN.



            Your expression is evaluated like



            alert((null || (2 && 3)) || 4);


            To get left-to-right evaluation, you will need to write



            alert((null || 2) && 3 || 4 );





            share|improve this answer




















            • 2





              It still logs 3 and even if the precedence of || and && were swapped it would still log 3.

              – Paulpro
              Mar 23 at 15:07















            1















            Is there higher priority of && over || operator?




            Yes. This is called operator precedence. You can find a table for JS operators at MDN.



            Your expression is evaluated like



            alert((null || (2 && 3)) || 4);


            To get left-to-right evaluation, you will need to write



            alert((null || 2) && 3 || 4 );





            share|improve this answer




















            • 2





              It still logs 3 and even if the precedence of || and && were swapped it would still log 3.

              – Paulpro
              Mar 23 at 15:07













            1












            1








            1








            Is there higher priority of && over || operator?




            Yes. This is called operator precedence. You can find a table for JS operators at MDN.



            Your expression is evaluated like



            alert((null || (2 && 3)) || 4);


            To get left-to-right evaluation, you will need to write



            alert((null || 2) && 3 || 4 );





            share|improve this answer
















            Is there higher priority of && over || operator?




            Yes. This is called operator precedence. You can find a table for JS operators at MDN.



            Your expression is evaluated like



            alert((null || (2 && 3)) || 4);


            To get left-to-right evaluation, you will need to write



            alert((null || 2) && 3 || 4 );






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 23 at 14:44

























            answered Mar 23 at 14:42









            BergiBergi

            387k64601928




            387k64601928







            • 2





              It still logs 3 and even if the precedence of || and && were swapped it would still log 3.

              – Paulpro
              Mar 23 at 15:07












            • 2





              It still logs 3 and even if the precedence of || and && were swapped it would still log 3.

              – Paulpro
              Mar 23 at 15:07







            2




            2





            It still logs 3 and even if the precedence of || and && were swapped it would still log 3.

            – Paulpro
            Mar 23 at 15:07





            It still logs 3 and even if the precedence of || and && were swapped it would still log 3.

            – Paulpro
            Mar 23 at 15:07











            1














            The order of evaluation is



            null || (2 && 3) || 4
            null || 3 || 4
            3 || 4
            3


            logical AND && has a higher operator precedence than logical OR ||.






            console.log(null || 2 && 3 || 4); // 3








            share|improve this answer




















            • 1





              Order of evaluation doesn't matter much here. It would log 3 no matter what order: (null||2)&&(3||4) or (null||2) && 3 || 4 all evaluate to 3

              – Paulpro
              Mar 23 at 15:09















            1














            The order of evaluation is



            null || (2 && 3) || 4
            null || 3 || 4
            3 || 4
            3


            logical AND && has a higher operator precedence than logical OR ||.






            console.log(null || 2 && 3 || 4); // 3








            share|improve this answer




















            • 1





              Order of evaluation doesn't matter much here. It would log 3 no matter what order: (null||2)&&(3||4) or (null||2) && 3 || 4 all evaluate to 3

              – Paulpro
              Mar 23 at 15:09













            1












            1








            1







            The order of evaluation is



            null || (2 && 3) || 4
            null || 3 || 4
            3 || 4
            3


            logical AND && has a higher operator precedence than logical OR ||.






            console.log(null || 2 && 3 || 4); // 3








            share|improve this answer















            The order of evaluation is



            null || (2 && 3) || 4
            null || 3 || 4
            3 || 4
            3


            logical AND && has a higher operator precedence than logical OR ||.






            console.log(null || 2 && 3 || 4); // 3








            console.log(null || 2 && 3 || 4); // 3





            console.log(null || 2 && 3 || 4); // 3






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 23 at 14:47

























            answered Mar 23 at 14:42









            Nina ScholzNina Scholz

            204k16117188




            204k16117188







            • 1





              Order of evaluation doesn't matter much here. It would log 3 no matter what order: (null||2)&&(3||4) or (null||2) && 3 || 4 all evaluate to 3

              – Paulpro
              Mar 23 at 15:09












            • 1





              Order of evaluation doesn't matter much here. It would log 3 no matter what order: (null||2)&&(3||4) or (null||2) && 3 || 4 all evaluate to 3

              – Paulpro
              Mar 23 at 15:09







            1




            1





            Order of evaluation doesn't matter much here. It would log 3 no matter what order: (null||2)&&(3||4) or (null||2) && 3 || 4 all evaluate to 3

            – Paulpro
            Mar 23 at 15:09





            Order of evaluation doesn't matter much here. It would log 3 no matter what order: (null||2)&&(3||4) or (null||2) && 3 || 4 all evaluate to 3

            – Paulpro
            Mar 23 at 15:09











            0














            The || operator runs next operand if it returns falsy value.



            The && operator runs next operand if it returns truthy value.



            Otherwise stop going further.



            So,



            - null || // false, go further
            - 2 && // true, go further
            - 3 || // true, stop


            Thus, finally returns 3.



            Edit:



            If you talk about priority of the operator, then you may look at the operator precedence. Where you can see && operator has precedence value with 6 and || operator has precedence value with 5. So, && operator has higher order.






            share|improve this answer

























            • Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.

              – Bergi
              Mar 23 at 18:00















            0














            The || operator runs next operand if it returns falsy value.



            The && operator runs next operand if it returns truthy value.



            Otherwise stop going further.



            So,



            - null || // false, go further
            - 2 && // true, go further
            - 3 || // true, stop


            Thus, finally returns 3.



            Edit:



            If you talk about priority of the operator, then you may look at the operator precedence. Where you can see && operator has precedence value with 6 and || operator has precedence value with 5. So, && operator has higher order.






            share|improve this answer

























            • Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.

              – Bergi
              Mar 23 at 18:00













            0












            0








            0







            The || operator runs next operand if it returns falsy value.



            The && operator runs next operand if it returns truthy value.



            Otherwise stop going further.



            So,



            - null || // false, go further
            - 2 && // true, go further
            - 3 || // true, stop


            Thus, finally returns 3.



            Edit:



            If you talk about priority of the operator, then you may look at the operator precedence. Where you can see && operator has precedence value with 6 and || operator has precedence value with 5. So, && operator has higher order.






            share|improve this answer















            The || operator runs next operand if it returns falsy value.



            The && operator runs next operand if it returns truthy value.



            Otherwise stop going further.



            So,



            - null || // false, go further
            - 2 && // true, go further
            - 3 || // true, stop


            Thus, finally returns 3.



            Edit:



            If you talk about priority of the operator, then you may look at the operator precedence. Where you can see && operator has precedence value with 6 and || operator has precedence value with 5. So, && operator has higher order.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 23 at 18:18

























            answered Mar 23 at 14:37









            Bhojendra RauniyarBhojendra Rauniyar

            53.7k2284136




            53.7k2284136












            • Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.

              – Bergi
              Mar 23 at 18:00

















            • Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.

              – Bergi
              Mar 23 at 18:00
















            Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.

            – Bergi
            Mar 23 at 18:00





            Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.

            – Bergi
            Mar 23 at 18:00

















            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%2f55314785%2fquestion-about-priority-of-logical-operators%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript