欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaScript中的true和false

程序员文章站 2024-02-26 19:49:04
...

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

JavaScript中的true和false

JavaScript中的true和false

JavaScript中的true和false

JavaScript中的true和false

<!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>