JavaScript中包括3个逻辑运算符,如下:
运算符 说明
&& 逻辑与,只有当两个操作数的值都为true时,a&&b 的值才为true
|| 逻辑或,只要两个操作数中其中之一的值为true,a||b的值就为true
! 逻辑非,!true的值为false,!false的值为true
逻辑运算符用于对逻辑操作数进行逻辑运算,而最典型的逻辑操作数就是条件表达式。
下面的例子:
<html>
<head>
<title>逻辑运算符</title>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
var a=2;
var b=3;
document.write("a=2,b=3<p>");
document.write("逻辑与:a<b && a<=b=");
document.write(a<b&&a<=b);
document.write("<BR>");
document.write("逻辑与:a<b && a>b=");
document.write(a<b&&a>b);
document.write("<BR>");
document.write("逻辑或:a<b || a>b=");
document.write(a<b||a>b);
document.write("<BR>");
document.write("逻辑或:a>b || a>=b=");
document.write(a>b||a>=b);
document.write("<BR>");
document.write("逻辑非:!(a<b)=");
document.write(!(a<b));
document.write("<BR>");
document.write("逻辑非:!(a>b)=");
document.write(!(a>b));
-->
</script>
</body>
</html>
运行结果如下图: