JavaScript中的true和false

https://ask.csdn.net/questions/1024438

https://www.cnblogs.com/lhyhappy365/p/6076512.html

https://www.cnblogs.com/sunmarvell/p/9048222.html

https://www.cnblogs.com/canger/p/6374185.html

https://segmentfault.com/q/1010000015470614/a-1020000015470827
undefined、NaN、0、null和空字符串''均被视为false
除上述以外的其它情况一律被视作true

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaScript中的true和false</title>
<script type="text/javascript">
/*
undefined、NaN、0、null和空字符串''均被视为false
除上述以外的其它情况一律被视作true
*/
var a = 1;
var b = 6;
var c = 8;
var d = 0;
//结果为true false false false
console.log(a==true, b==true, c==true, d==true);
//结果为false false false false
console.log(a===true, b===true, c===true, d===true);
//结果为false false false true
console.log(!a, !b, !c, !d);
//结果为false true false false false true false
console.log(true==0, true==1, true==2, true==3, true==666, false==0, false==1);
//结果为false truehello
console.log(true == 1 + 'hello', (true == 1) + 'hello');
//结果为true false
console.log(true == '1', true == 'true');

console.log('==================');
//结果为false false false
console.log(Boolean(false),Boolean(undefined),Boolean(null));
//结果为false false false
console.log(Boolean(0),Boolean(NaN),Boolean(''));
//结果为true true
console.log(Boolean([]),Boolean({}));
//结果为number number
console.log(typeof 1.0, typeof 1);
//js中没有整数和浮点数,只有number类型,1和1.0都是number类型
//结果为true true
console.log(1.0===1, 1.0==1);

var e = 1;
if(!e){ //判断条件是false,所以无法进入判断语句内部,那么f无法被赋值
	var f = 10;
	console.log('******************');
	console.log(f);
}
if(e){ //判断条件是true,所以进入判断语句内部,那么g会被赋值
	var g = 20;
	console.log('################');
	console.log(g);
}

var k = 8899;
if(k){ //判断条件是true,所以进入判断语句内部,那么m会被赋值
	var m = '江西省赣州市于都县';
	console.log('----------------');
	console.log(m);
}

console.log(f, g, m); //结果为undefined 20 江西省赣州市于都县

</script>
</head>
<body style="background-color: #CCE8CF;">
<h3 style="color: #cd1636;">JavaScript中的true和false</h3>
<a href="https://www.cnblogs.com/lhyhappy365/p/6076512.html" target="_blank">
参考网页https://www.cnblogs.com/lhyhappy365/p/6076512.html
</a>
<br/>
<a href="https://www.cnblogs.com/sunmarvell/p/9048222.html" target="_blank">
参考网页https://www.cnblogs.com/sunmarvell/p/9048222.html
</a>
<br/>
<a href="https://segmentfault.com/q/1010000015470614/a-1020000015470827" target="_blank">
参考网页https://segmentfault.com/q/1010000015470614/a-1020000015470827
</a>
<br/>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值