JavaScript 比較 和 邏輯運算符
比較運算子
運算子? ?說明? ? ? ?#範例? ? ? ?運算運算結果
== ? ?等於? ?2 == 3 ? ?FALSE ? ?
##=== ? ?恆等於(數(shù)值與類型都要做比較) ?##=== ? ?恆等於(數(shù)值與類型都要做比較) ? 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 也包含了基於某些條件對變數(shù)進行賦值的條件運算子。
語法
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)?"年齡太小,繼續(xù)幼兒園":"年齡已達到,可以入學"; document.getElementById("demo").innerHTML=voteable; } </script> </body> </html>