20.20.尚硅谷_JS基础_ 非布尔值的与或运算
一、&& 和 || 非布尔值的情况:
1、&&与运算
(1)true和true
- 如果两个值都为true,则返回后边的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
//true && true
//与运算:如果两个值都为true,则返回后边的。
var a = 1 && 2;
console.log("a = " + a)
var b = 2 && 1;
console.log("b = " + b)
</script>
</head>
<body>
</body>
</html>

(2)false和false
如果两个值都为false,则返回靠前的false
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var a = NaN && 0;
console.log("a = " + a)
var b = 0 && NaN;
console.log("b = " + b)
</script>
</head>
<body>
</body>
</html>

(3)false和true
如果一个值为true,一个值为false,则返回0。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var a = 1 && 0;
console.log("a = " + a)
var b = 0 && 1;
console.log("b = " + b)
</script>
</head>
<body>
</body>
</html>

2、||或运算:
(1)true
如果第一个值为true,则直接返回第一个值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
a = 2 || 1;
console.log("a = " + a)
a = 1 || 2;
console.log("a = " + a)
</script>
</head>
<body>
</body>
</html>

(2)false:
如果第一个值为false,则返回第二个值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
a = NaN || 1;
console.log("a = " + a)
a = NaN || 0 ;
console.log("a = " + a)
</script>
</head>
<body>
</body>
</html>
