PHP前端开发

JS |真与假|

百变鹏仔 2个月前 (10-14) #JavaScript
文章标签 JS

真值和假值:

在 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     flet cash =255  //0 ;     //conditions false  statement block not runif (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