JavaScript 比較 和 邏輯運算符
比較運算子
運算子 說明 #範例 運算運算結果
== 等於 2 == 3 FALSE
##=== 恆等於(數值與類型都要做比較)##=== 恆等於(數值與類型都要做比較) 2 (2 === 2 (2 === 2 (2 === 2 (2 === 2 (2 === 2 (2 === 2 (2 ===) TRUE) (2 === "2" FALSE )
!= 不等於,也可寫<> 2 == 3 TRUE 可寫 > 2 ==
< 小於 2 < 3 TRUE
>= 大於等於 2 >= #
比較運算子也可用於字串比較。
<!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 =
&& 邏輯與(and) x =
<!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>
新建檔案
<!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>
預覽
Clear
- 課程推薦
- 課件下載
課件暫不提供下載,工作人員正在整理中,後期請多關注該課程~ 















