In if statement,undefined equals with false












7














I'm confused with the code below:



if(undefined){
//code will not be executed
}


and



if(!undefined){
//code will be executed
}


Is that mean the "undefined" equals with false?



Here the question related,but no one point above situation out.










share|improve this question




















  • 3




    The truthy value of undefined in Javascript is false. Hence the behaviour that you see.
    – stackErr
    May 11 '16 at 15:42








  • 1




    Boolean(undefined) is false.but your comments should be interchanged
    – Madhawa Priyashantha
    May 11 '16 at 15:43












  • @FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
    – Xheldon Cao
    May 12 '16 at 2:25










  • @XheldonCao if you pass a none Boolean value then yes.javascript use Boolean() function to get a boolean value because if condition need a boolean value
    – Madhawa Priyashantha
    May 12 '16 at 3:18










  • @FastSnail Now I know,thanks guys!
    – Xheldon Cao
    May 12 '16 at 3:44
















7














I'm confused with the code below:



if(undefined){
//code will not be executed
}


and



if(!undefined){
//code will be executed
}


Is that mean the "undefined" equals with false?



Here the question related,but no one point above situation out.










share|improve this question




















  • 3




    The truthy value of undefined in Javascript is false. Hence the behaviour that you see.
    – stackErr
    May 11 '16 at 15:42








  • 1




    Boolean(undefined) is false.but your comments should be interchanged
    – Madhawa Priyashantha
    May 11 '16 at 15:43












  • @FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
    – Xheldon Cao
    May 12 '16 at 2:25










  • @XheldonCao if you pass a none Boolean value then yes.javascript use Boolean() function to get a boolean value because if condition need a boolean value
    – Madhawa Priyashantha
    May 12 '16 at 3:18










  • @FastSnail Now I know,thanks guys!
    – Xheldon Cao
    May 12 '16 at 3:44














7












7








7


2





I'm confused with the code below:



if(undefined){
//code will not be executed
}


and



if(!undefined){
//code will be executed
}


Is that mean the "undefined" equals with false?



Here the question related,but no one point above situation out.










share|improve this question















I'm confused with the code below:



if(undefined){
//code will not be executed
}


and



if(!undefined){
//code will be executed
}


Is that mean the "undefined" equals with false?



Here the question related,but no one point above situation out.







javascript if-statement undefined






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 10:31









Community

11




11










asked May 11 '16 at 15:40









Xheldon Cao

779




779








  • 3




    The truthy value of undefined in Javascript is false. Hence the behaviour that you see.
    – stackErr
    May 11 '16 at 15:42








  • 1




    Boolean(undefined) is false.but your comments should be interchanged
    – Madhawa Priyashantha
    May 11 '16 at 15:43












  • @FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
    – Xheldon Cao
    May 12 '16 at 2:25










  • @XheldonCao if you pass a none Boolean value then yes.javascript use Boolean() function to get a boolean value because if condition need a boolean value
    – Madhawa Priyashantha
    May 12 '16 at 3:18










  • @FastSnail Now I know,thanks guys!
    – Xheldon Cao
    May 12 '16 at 3:44














  • 3




    The truthy value of undefined in Javascript is false. Hence the behaviour that you see.
    – stackErr
    May 11 '16 at 15:42








  • 1




    Boolean(undefined) is false.but your comments should be interchanged
    – Madhawa Priyashantha
    May 11 '16 at 15:43












  • @FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
    – Xheldon Cao
    May 12 '16 at 2:25










  • @XheldonCao if you pass a none Boolean value then yes.javascript use Boolean() function to get a boolean value because if condition need a boolean value
    – Madhawa Priyashantha
    May 12 '16 at 3:18










  • @FastSnail Now I know,thanks guys!
    – Xheldon Cao
    May 12 '16 at 3:44








3




3




The truthy value of undefined in Javascript is false. Hence the behaviour that you see.
– stackErr
May 11 '16 at 15:42






The truthy value of undefined in Javascript is false. Hence the behaviour that you see.
– stackErr
May 11 '16 at 15:42






1




1




Boolean(undefined) is false.but your comments should be interchanged
– Madhawa Priyashantha
May 11 '16 at 15:43






Boolean(undefined) is false.but your comments should be interchanged
– Madhawa Priyashantha
May 11 '16 at 15:43














@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25




@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25












@XheldonCao if you pass a none Boolean value then yes.javascript use Boolean() function to get a boolean value because if condition need a boolean value
– Madhawa Priyashantha
May 12 '16 at 3:18




@XheldonCao if you pass a none Boolean value then yes.javascript use Boolean() function to get a boolean value because if condition need a boolean value
– Madhawa Priyashantha
May 12 '16 at 3:18












@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44




@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44












3 Answers
3






active

oldest

votes


















16














It means that undefined is a falsy value, list of falsy values are:



""        # Empty string
null # null
undefined # undefined, which you get when doing: var a;
false # Boolean false
0 # Number 0
NaN # Not A Number eg: "a" * 2


If you negate a falsy value you will get true:



!""        === true
!null === true
!undefined === true
!0 === true
!NaN === true


And when you nagate a truly value you will get false:



!"hello" === false
!1 === false


But undefined is not equal false:



undefined === false // false
undefined == false // false


And just for the fun if it:



undefined == null // true





share|improve this answer























  • I will write it down my memorandum,thanks
    – Xheldon Cao
    May 12 '16 at 2:46



















5














In javascript strict mode, undefined is not false, but javascript try to convert the object or var to a boolean value (this is called in javascript truthy value), that's the reason you got an undefined as false. This happens with null also, for example.



You can force that with this strict no equality:



if(undefined!==false) console.log("Is not false"); 





share|improve this answer





























    2














    Please take a look below checked falsy values:



    ""==false?
    Ans: true
    null == false?
    Ans: false
    undefined == false?
    Ans: false
    0 == false?
    Ans: true
    NaN == false?
    Ans: false
    null == NaN?
    Ans: false


    We can see that null == false,undefined == false,null == NaN, and NaN == false are not true
    That means they are not equal. From the above result, we got 3 falsy values group:




    1. The False group

    2. The Null group and

    3. The NaN group


    But a negative falsy value is always true:



    !""        === true
    !null === true
    !undefined === true
    !0 === true
    !NaN === true


    For example:
    To check true value of dataTitle variable



    if(dataTitle && (dataTitle != null))
    {
    console.log('hi');
    }


    The above statement will check the false group as well as the null group



    To check false value of dataTitle variable



    if(!dataTitle)
    {
    console.log('hi');
    }





    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%2f37167380%2fin-if-statement-undefined-equals-with-false%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      16














      It means that undefined is a falsy value, list of falsy values are:



      ""        # Empty string
      null # null
      undefined # undefined, which you get when doing: var a;
      false # Boolean false
      0 # Number 0
      NaN # Not A Number eg: "a" * 2


      If you negate a falsy value you will get true:



      !""        === true
      !null === true
      !undefined === true
      !0 === true
      !NaN === true


      And when you nagate a truly value you will get false:



      !"hello" === false
      !1 === false


      But undefined is not equal false:



      undefined === false // false
      undefined == false // false


      And just for the fun if it:



      undefined == null // true





      share|improve this answer























      • I will write it down my memorandum,thanks
        – Xheldon Cao
        May 12 '16 at 2:46
















      16














      It means that undefined is a falsy value, list of falsy values are:



      ""        # Empty string
      null # null
      undefined # undefined, which you get when doing: var a;
      false # Boolean false
      0 # Number 0
      NaN # Not A Number eg: "a" * 2


      If you negate a falsy value you will get true:



      !""        === true
      !null === true
      !undefined === true
      !0 === true
      !NaN === true


      And when you nagate a truly value you will get false:



      !"hello" === false
      !1 === false


      But undefined is not equal false:



      undefined === false // false
      undefined == false // false


      And just for the fun if it:



      undefined == null // true





      share|improve this answer























      • I will write it down my memorandum,thanks
        – Xheldon Cao
        May 12 '16 at 2:46














      16












      16








      16






      It means that undefined is a falsy value, list of falsy values are:



      ""        # Empty string
      null # null
      undefined # undefined, which you get when doing: var a;
      false # Boolean false
      0 # Number 0
      NaN # Not A Number eg: "a" * 2


      If you negate a falsy value you will get true:



      !""        === true
      !null === true
      !undefined === true
      !0 === true
      !NaN === true


      And when you nagate a truly value you will get false:



      !"hello" === false
      !1 === false


      But undefined is not equal false:



      undefined === false // false
      undefined == false // false


      And just for the fun if it:



      undefined == null // true





      share|improve this answer














      It means that undefined is a falsy value, list of falsy values are:



      ""        # Empty string
      null # null
      undefined # undefined, which you get when doing: var a;
      false # Boolean false
      0 # Number 0
      NaN # Not A Number eg: "a" * 2


      If you negate a falsy value you will get true:



      !""        === true
      !null === true
      !undefined === true
      !0 === true
      !NaN === true


      And when you nagate a truly value you will get false:



      !"hello" === false
      !1 === false


      But undefined is not equal false:



      undefined === false // false
      undefined == false // false


      And just for the fun if it:



      undefined == null // true






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 11 '16 at 16:05

























      answered May 11 '16 at 15:43









      andlrc

      35.5k126498




      35.5k126498












      • I will write it down my memorandum,thanks
        – Xheldon Cao
        May 12 '16 at 2:46


















      • I will write it down my memorandum,thanks
        – Xheldon Cao
        May 12 '16 at 2:46
















      I will write it down my memorandum,thanks
      – Xheldon Cao
      May 12 '16 at 2:46




      I will write it down my memorandum,thanks
      – Xheldon Cao
      May 12 '16 at 2:46













      5














      In javascript strict mode, undefined is not false, but javascript try to convert the object or var to a boolean value (this is called in javascript truthy value), that's the reason you got an undefined as false. This happens with null also, for example.



      You can force that with this strict no equality:



      if(undefined!==false) console.log("Is not false"); 





      share|improve this answer


























        5














        In javascript strict mode, undefined is not false, but javascript try to convert the object or var to a boolean value (this is called in javascript truthy value), that's the reason you got an undefined as false. This happens with null also, for example.



        You can force that with this strict no equality:



        if(undefined!==false) console.log("Is not false"); 





        share|improve this answer
























          5












          5








          5






          In javascript strict mode, undefined is not false, but javascript try to convert the object or var to a boolean value (this is called in javascript truthy value), that's the reason you got an undefined as false. This happens with null also, for example.



          You can force that with this strict no equality:



          if(undefined!==false) console.log("Is not false"); 





          share|improve this answer












          In javascript strict mode, undefined is not false, but javascript try to convert the object or var to a boolean value (this is called in javascript truthy value), that's the reason you got an undefined as false. This happens with null also, for example.



          You can force that with this strict no equality:



          if(undefined!==false) console.log("Is not false"); 






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 11 '16 at 15:47









          Christopher Díaz

          31817




          31817























              2














              Please take a look below checked falsy values:



              ""==false?
              Ans: true
              null == false?
              Ans: false
              undefined == false?
              Ans: false
              0 == false?
              Ans: true
              NaN == false?
              Ans: false
              null == NaN?
              Ans: false


              We can see that null == false,undefined == false,null == NaN, and NaN == false are not true
              That means they are not equal. From the above result, we got 3 falsy values group:




              1. The False group

              2. The Null group and

              3. The NaN group


              But a negative falsy value is always true:



              !""        === true
              !null === true
              !undefined === true
              !0 === true
              !NaN === true


              For example:
              To check true value of dataTitle variable



              if(dataTitle && (dataTitle != null))
              {
              console.log('hi');
              }


              The above statement will check the false group as well as the null group



              To check false value of dataTitle variable



              if(!dataTitle)
              {
              console.log('hi');
              }





              share|improve this answer




























                2














                Please take a look below checked falsy values:



                ""==false?
                Ans: true
                null == false?
                Ans: false
                undefined == false?
                Ans: false
                0 == false?
                Ans: true
                NaN == false?
                Ans: false
                null == NaN?
                Ans: false


                We can see that null == false,undefined == false,null == NaN, and NaN == false are not true
                That means they are not equal. From the above result, we got 3 falsy values group:




                1. The False group

                2. The Null group and

                3. The NaN group


                But a negative falsy value is always true:



                !""        === true
                !null === true
                !undefined === true
                !0 === true
                !NaN === true


                For example:
                To check true value of dataTitle variable



                if(dataTitle && (dataTitle != null))
                {
                console.log('hi');
                }


                The above statement will check the false group as well as the null group



                To check false value of dataTitle variable



                if(!dataTitle)
                {
                console.log('hi');
                }





                share|improve this answer


























                  2












                  2








                  2






                  Please take a look below checked falsy values:



                  ""==false?
                  Ans: true
                  null == false?
                  Ans: false
                  undefined == false?
                  Ans: false
                  0 == false?
                  Ans: true
                  NaN == false?
                  Ans: false
                  null == NaN?
                  Ans: false


                  We can see that null == false,undefined == false,null == NaN, and NaN == false are not true
                  That means they are not equal. From the above result, we got 3 falsy values group:




                  1. The False group

                  2. The Null group and

                  3. The NaN group


                  But a negative falsy value is always true:



                  !""        === true
                  !null === true
                  !undefined === true
                  !0 === true
                  !NaN === true


                  For example:
                  To check true value of dataTitle variable



                  if(dataTitle && (dataTitle != null))
                  {
                  console.log('hi');
                  }


                  The above statement will check the false group as well as the null group



                  To check false value of dataTitle variable



                  if(!dataTitle)
                  {
                  console.log('hi');
                  }





                  share|improve this answer














                  Please take a look below checked falsy values:



                  ""==false?
                  Ans: true
                  null == false?
                  Ans: false
                  undefined == false?
                  Ans: false
                  0 == false?
                  Ans: true
                  NaN == false?
                  Ans: false
                  null == NaN?
                  Ans: false


                  We can see that null == false,undefined == false,null == NaN, and NaN == false are not true
                  That means they are not equal. From the above result, we got 3 falsy values group:




                  1. The False group

                  2. The Null group and

                  3. The NaN group


                  But a negative falsy value is always true:



                  !""        === true
                  !null === true
                  !undefined === true
                  !0 === true
                  !NaN === true


                  For example:
                  To check true value of dataTitle variable



                  if(dataTitle && (dataTitle != null))
                  {
                  console.log('hi');
                  }


                  The above statement will check the false group as well as the null group



                  To check false value of dataTitle variable



                  if(!dataTitle)
                  {
                  console.log('hi');
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 10:11

























                  answered Nov 20 at 9:04









                  Bablu Ahmed

                  940720




                  940720






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f37167380%2fin-if-statement-undefined-equals-with-false%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Fotorealismo