php中文网

JS |真与假|

php中文网

真值和假值:

在 javascript 中,真值和假值用于确定布尔值中的值是 true 还是 false
context.这个概念对于使用 if 语句等条件控制程序流程至关重要。

falsy values: 0," ",null,nan,false,undefined 
console.log(boolean(0)); //false
console.log(boolean(undefined)); //false

console.log(boolean(' '));  //empty false

console.log(boolean(null)); //false

console.log(boolean(nan)); //false not a number

console.log(boolean(false)); //false

真值:任何非假值:

console.log(boolean(1)); //true
console.log(boolean(1833933)); //true
console.log(boolean(-1)); //true
console.log(boolean("hello")); //true
console.log(boolean(1)); //true
console.log(boolean([])); //true empty array
console.log(boolean({})); //true empty object
console.log(function (){}); //true
example:
           t     f
let cash =255  //0 ; 
    //conditions false  statement block not run
if (cash){
  console.log("you can buy burger with drink"); 
}else{
   console.log("you can buy burger"); 
}else{
   console.log("you don't have money"); 
}
let a;
console.log(a); //false
output:
undefined 
let a = 10;
console.log(a); //true
let a = null;
console.log(a); //false

以上就是JS |真与假|的详细内容,更多请关注php中文网其它相关文章!