JavaScript 比较 和 逻辑运算符
比较运算符
运算符 说明 例子 运算结果
== 等于 2 == 3 FALSE
=== 恒等于(值和类型都要做比较) (2 === 2 TRUE) (2 === "2" FALSE )
!= 不等于,也可写作<> 2 == 3 TRUE
> 大于 2 > 3 FALSE
< 小于 2 < 3 TRUE
>= 大于等于 2 >= 3 FALSE
<= 小于等于 2 <= 3 TRUE
比较运算符也可用于字符串比较。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
<script type="text/javascript">
var a=5,b="5";
document.write(a==b);
document.write("<br />");
document.write(a===b);
document.write("<br />");
document.write(a!=b);
document.write("<br />");
document.write(a!==b);
</script>
</head>
<body>
</body>
</html>逻辑运算符
运算符 说明 例子 运算结果
&& 逻辑与(and) x = 2; y = 6; x && y > 5 FALSE
|| 逻辑或(or) x = 2; y = 6; x || y > 5 TRUE
! 逻辑非,取逻辑的反面 x = 2; y = 6; !(x > y) TRUE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
<script type="text/javascript">
var a=5,b="5";
document.write(a&&b > 10);
document.write("<br />");
document.write(a||b <6);
document.write("<br />");
document.write(!(a>b));
</script>
</head>
<body>
</body>
</html>条件运算符
JavaScript 还包含了基于某些条件对变量进行赋值的条件运算符。
语法
variablename=(condition)?value1:value2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
年龄:<input id="age" value="18" />
<p>是否达到上学年龄?</p>
<button onclick="myFunction()">点击按钮</button>
<p id="demo"></p>
<script>
function myFunction()
{
var age,voteable;
age=document.getElementById("age").value;
voteable=(age<7)?"年龄太小,继续幼儿园":"年龄已达到,可以入学";
document.getElementById("demo").innerHTML=voteable;
}
</script>
</body>
</html>

我喜欢晴天
逻辑运算符不大理解啊
8年前 添加回复 0