All falsey values in JavaScript

前端 未结 4 1875
囚心锁ツ
囚心锁ツ 2020-11-21 07:21

What are the values in JavaScript that are \'falsey\', meaning that they evaluate as false in expressions like if(value), value ?

4条回答
  •  春和景丽
    2020-11-21 07:47

    Just to add to @user568458's list of falsy values:

    • In addition to integer number 0, the decimal number 0.0, 0.00 or any such zeroish number is also a falsy value.

      var myNum = 0.0;
      if(myNum){
          console.log('I am a truthy value');
      }
      else {
          console.log('I am a falsy value');
      }
      

      Above code snippet prints I am a falsy value

    • Similarly hex representation of the number 0 is also a falsy value as shown in below code snippet:

      var myNum = 0x0; //hex representation of 0
      if(myNum){
          console.log('I am a truthy value');
      }   
      else {
          console.log('I am a falsy value');
      }
      

      Above code snippet again prints I am a falsy value.

提交回复
热议问题