Javascript Falsy Check execution order in an IF condition The 2019 Stack Overflow Developer Survey Results Are InJavaScript check if variable exists (is defined/initialized)How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Setting “checked” for a checkbox with jQuery?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Time travel alters history but people keep saying nothing's changed

Inversion Puzzle

What do the Banks children have against barley water?

Dual Citizen. Exited the US on Italian passport recently

I see my dog run

Are there any other methods to apply to solving simultaneous equations?

How come people say “Would of”?

Geography at the pixel level

Springs with some finite mass

What does "sndry explns" mean in one of the Hitchhiker's guide books?

Why isn't airport relocation done gradually?

Idiomatic way to prevent slicing?

What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

Is "plugging out" electronic devices an American expression?

aging parents with no investments

Why did Howard Stark use all the Vibranium they had on a prototype shield?

How to change the limits of integration

Why is Grand Jury testimony secret?

Why do I get badly formatted numerical results when I use StringForm?

Falsification in Math vs Science

What is the motivation for a law requiring 2 parties to consent for recording a conversation

Why can Shazam do this?

Pristine Bit Checking



Javascript Falsy Check execution order in an IF condition



The 2019 Stack Overflow Developer Survey Results Are InJavaScript check if variable exists (is defined/initialized)How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Setting “checked” for a checkbox with jQuery?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?



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








-2















I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error and Data.



const data, error = response;


If there is no error, the Error object becomes null. Consider the following two options.



Option One:



if (!error) 
// handle error



Option Two:



if (error !== null) 
// handle error



In Option One, As per this answer, the following conditions are checked.




  1. undefined: if the value is not defined and it's undefined


  2. null: if it's null, for example, if a DOM element not exists...


  3. empty string: ''


  4. 0: number zero


  5. NaN: not a number

  6. false

Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?










share|improve this question

















  • 5





    async vs speed of a micro optimization? really?

    – Nina Scholz
    Mar 8 at 8:53







  • 1





    Also I think you mean something else than if (!error) // handle error

    – mplungjan
    Mar 8 at 8:56






  • 3





    This must be the most micro of premature optimisations I've ever seen.

    – VLAZ
    Mar 8 at 8:58











  • Interestingly the destructuring assignment is probably the slowest part in this example...

    – Yoshi
    Mar 8 at 9:02











  • I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?

    – Keet Sugathadasa
    Mar 8 at 9:04


















-2















I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error and Data.



const data, error = response;


If there is no error, the Error object becomes null. Consider the following two options.



Option One:



if (!error) 
// handle error



Option Two:



if (error !== null) 
// handle error



In Option One, As per this answer, the following conditions are checked.




  1. undefined: if the value is not defined and it's undefined


  2. null: if it's null, for example, if a DOM element not exists...


  3. empty string: ''


  4. 0: number zero


  5. NaN: not a number

  6. false

Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?










share|improve this question

















  • 5





    async vs speed of a micro optimization? really?

    – Nina Scholz
    Mar 8 at 8:53







  • 1





    Also I think you mean something else than if (!error) // handle error

    – mplungjan
    Mar 8 at 8:56






  • 3





    This must be the most micro of premature optimisations I've ever seen.

    – VLAZ
    Mar 8 at 8:58











  • Interestingly the destructuring assignment is probably the slowest part in this example...

    – Yoshi
    Mar 8 at 9:02











  • I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?

    – Keet Sugathadasa
    Mar 8 at 9:04














-2












-2








-2








I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error and Data.



const data, error = response;


If there is no error, the Error object becomes null. Consider the following two options.



Option One:



if (!error) 
// handle error



Option Two:



if (error !== null) 
// handle error



In Option One, As per this answer, the following conditions are checked.




  1. undefined: if the value is not defined and it's undefined


  2. null: if it's null, for example, if a DOM element not exists...


  3. empty string: ''


  4. 0: number zero


  5. NaN: not a number

  6. false

Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?










share|improve this question














I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error and Data.



const data, error = response;


If there is no error, the Error object becomes null. Consider the following two options.



Option One:



if (!error) 
// handle error



Option Two:



if (error !== null) 
// handle error



In Option One, As per this answer, the following conditions are checked.




  1. undefined: if the value is not defined and it's undefined


  2. null: if it's null, for example, if a DOM element not exists...


  3. empty string: ''


  4. 0: number zero


  5. NaN: not a number

  6. false

Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 8:51









Keet SugathadasaKeet Sugathadasa

725621




725621







  • 5





    async vs speed of a micro optimization? really?

    – Nina Scholz
    Mar 8 at 8:53







  • 1





    Also I think you mean something else than if (!error) // handle error

    – mplungjan
    Mar 8 at 8:56






  • 3





    This must be the most micro of premature optimisations I've ever seen.

    – VLAZ
    Mar 8 at 8:58











  • Interestingly the destructuring assignment is probably the slowest part in this example...

    – Yoshi
    Mar 8 at 9:02











  • I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?

    – Keet Sugathadasa
    Mar 8 at 9:04













  • 5





    async vs speed of a micro optimization? really?

    – Nina Scholz
    Mar 8 at 8:53







  • 1





    Also I think you mean something else than if (!error) // handle error

    – mplungjan
    Mar 8 at 8:56






  • 3





    This must be the most micro of premature optimisations I've ever seen.

    – VLAZ
    Mar 8 at 8:58











  • Interestingly the destructuring assignment is probably the slowest part in this example...

    – Yoshi
    Mar 8 at 9:02











  • I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?

    – Keet Sugathadasa
    Mar 8 at 9:04








5




5





async vs speed of a micro optimization? really?

– Nina Scholz
Mar 8 at 8:53






async vs speed of a micro optimization? really?

– Nina Scholz
Mar 8 at 8:53





1




1





Also I think you mean something else than if (!error) // handle error

– mplungjan
Mar 8 at 8:56





Also I think you mean something else than if (!error) // handle error

– mplungjan
Mar 8 at 8:56




3




3





This must be the most micro of premature optimisations I've ever seen.

– VLAZ
Mar 8 at 8:58





This must be the most micro of premature optimisations I've ever seen.

– VLAZ
Mar 8 at 8:58













Interestingly the destructuring assignment is probably the slowest part in this example...

– Yoshi
Mar 8 at 9:02





Interestingly the destructuring assignment is probably the slowest part in this example...

– Yoshi
Mar 8 at 9:02













I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?

– Keet Sugathadasa
Mar 8 at 9:04






I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?

– Keet Sugathadasa
Mar 8 at 9:04













1 Answer
1






active

oldest

votes


















1














There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy



Falsey results:



if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
if (document.all)
// => false


Truthy results:



if (true)
if ()
if ([])
if (42)
if ("foo")
if new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
// => true





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%2f55059621%2fjavascript-falsy-check-execution-order-in-an-if-condition%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy



    Falsey results:



    if (false)
    if (null)
    if (undefined)
    if (0)
    if (NaN)
    if ('')
    if ("")
    if (``)
    if (document.all)
    // => false


    Truthy results:



    if (true)
    if ()
    if ([])
    if (42)
    if ("foo")
    if new Date())
    if (-42)
    if (3.14)
    if (-3.14)
    if (Infinity)
    if (-Infinity)
    // => true





    share|improve this answer





























      1














      There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy



      Falsey results:



      if (false)
      if (null)
      if (undefined)
      if (0)
      if (NaN)
      if ('')
      if ("")
      if (``)
      if (document.all)
      // => false


      Truthy results:



      if (true)
      if ()
      if ([])
      if (42)
      if ("foo")
      if new Date())
      if (-42)
      if (3.14)
      if (-3.14)
      if (Infinity)
      if (-Infinity)
      // => true





      share|improve this answer



























        1












        1








        1







        There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy



        Falsey results:



        if (false)
        if (null)
        if (undefined)
        if (0)
        if (NaN)
        if ('')
        if ("")
        if (``)
        if (document.all)
        // => false


        Truthy results:



        if (true)
        if ()
        if ([])
        if (42)
        if ("foo")
        if new Date())
        if (-42)
        if (3.14)
        if (-3.14)
        if (Infinity)
        if (-Infinity)
        // => true





        share|improve this answer















        There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy



        Falsey results:



        if (false)
        if (null)
        if (undefined)
        if (0)
        if (NaN)
        if ('')
        if ("")
        if (``)
        if (document.all)
        // => false


        Truthy results:



        if (true)
        if ()
        if ([])
        if (42)
        if ("foo")
        if new Date())
        if (-42)
        if (3.14)
        if (-3.14)
        if (Infinity)
        if (-Infinity)
        // => true






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 9:04

























        answered Mar 8 at 8:58









        Branislav PopadičBranislav Popadič

        214




        214





























            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%2f55059621%2fjavascript-falsy-check-execution-order-in-an-if-condition%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

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко