JavaScript支持以下逻辑运算符。假设变量A持有10,变量B持有20,那么,
序号 | 运算符和说明 |
---|---|
1 | &&(逻辑与) 如果两个操作数都不为零,则条件变为true。 例如:(A && B)是真的。 |
2 | | | (逻辑或) 如果两个操作数中的任何一个都不为零,则条件变为true。 例如:(A || B)为真。 |
3 | !(逻辑非) 反转其操作数的逻辑状态。如果条件为真,则逻辑非运算符会将其设置为假。 例如: !(A && B)是错误的。 |
您可以尝试以下代码来学习如何在JavaScript中实现逻辑运算符-
<html> <body> <script> <!-- var a = true; var b = false; var linebreak = "<br />"; document.write("(a && b) => "); result = (a && b); document.write(result); document.write(linebreak); document.write("(a || b) => "); result = (a || b); document.write(result); document.write(linebreak); document.write("!(a && b) => "); result = (!(a && b)); document.write(result); document.write(linebreak); //--> </script> <p>Set the variables to different values and different operators and then try...</p> </body> </html>