JavaScript 比較
比較和邏輯運(yùn)算符用于測試 true 或者false。
比較運(yùn)算符
比較運(yùn)算符在邏輯語句中使用,以測定變量或值是否相等。
x=5,下面的表格解釋了比較運(yùn)算符:
==:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x==8 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x==8; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x==5 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x==5; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
===:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x==="5" 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x==="5"; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x===5 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x===5; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
!=:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x!=8 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x!=8; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
!==:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x!=="5" 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x!=="5"; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x!==5 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x!==5; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
>:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x>8 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x>8; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x<8 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x<8; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
>=:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x>=8 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x>=8; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<=:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>x=5, 返回 x<=8 的比較值結(jié)果。</p> <button onclick="myFunction()">嘗試一下</button> <p id="demo"></p> <script> function myFunction() { var x=5; document.getElementById("demo").innerHTML=x<=8; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
如何使用
可以在條件語句中使用比較運(yùn)算符對值進(jìn)行比較,然后根據(jù)結(jié)果來采取行動(dòng):
您將在本教程的下一節(jié)中學(xué)習(xí)更多有關(guān)條件語句的知識(shí)。
邏輯運(yùn)算符
邏輯運(yùn)算符用于測定變量或值之間的邏輯。
給定 x=6 以及 y=3,下表解釋了邏輯運(yùn)算符:
運(yùn)算符 | 描述 | 例子 |
---|---|---|
&& | and | (x < 10 && y > 1) 為 true |
|| | or | (x==5 || y==5) 為 false |
! | not | !(x==y) 為 true |
條件運(yùn)算符
JavaScript 還包含了基于某些條件對變量進(jìn)行賦值的條件運(yùn)算符。
語法
例子
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕檢測年齡。</p> 年齡:<input id="age" value="18" /> <p>是否達(dá)到投票年齡?</p> <button onclick="myFunction()">點(diǎn)擊按鈕</button> <p id="demo"></p> <script> function myFunction() { var age,voteable; age=document.getElementById("age").value; voteable=(age<18)?"年齡太小":"年齡已達(dá)到"; document.getElementById("demo").innerHTML=voteable; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例