真值和假值:
在 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中文网其它相关文章!
版权声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系 yyfuon@163.com