真值和假值:
在 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中文网其他相关文章!