1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <!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,则必然返回第二个值
- -如果第一个值为false,则直接返回第一个值
- -或运算
- -如果第一个值为true,则直接返回第一个值
- -如果第一个值为false,则返回第二个值
- */
- var result = 1 && 2;//相当于true && true
- //与运算:如果两个值都为true,则返回后边的
- console.log(result);//输出结果为2
- //与运算:如果两个值中有false,则返回靠前的false
- // false && true、true && false结构
- result = 0 && 2;//输出结果为0
- result = 2 && 0;////输出结果为0
- //false && false结构
- result = NaN && 0;//输出结果为NaN
- result = 0 && NaN;//输出结果为0
- // console.log(result);//输出结果为0
- //true || true
- //如果第一个值为true,则直接返回第一个值
- result = 1||2;//结果为1
- //如果第一个值为false,则直接返回第二个值
- result = NaN || 1;//输出结果为1
- result = NaN || 0;//输出结果为0
- result = "" || "hello";//输出结果为hello
- result = -1 || "你好";//输出结果为-1
- console.log(result);
- </script>
- </head>
- <body>
-
- </body>
- </html>
|