📄 js_comparisons.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Comparison and Logical Operators</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>JavaScript Comparison and Logical Operators</h1>
<a href="js_operators.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_if_else.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">Comparison and Logical operators are used to test for true or
false.</p>
<hr />
<h2>Comparison Operators</h2>
<p>Comparison operators are used in logical statements to determine equality or
difference between variables or values. </p>
<p>Given that <b>x=5</b>, the table below explains the comparison operators:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table1">
<tr>
<th width="15%" align="left">Operator</th>
<th width="45%" align="left">Description</th>
<th width="40%" align="left">Example</th>
</tr>
<tr>
<td valign="top">==</td>
<td valign="top">is equal to
</td>
<td valign="top">x==8 is false
</td>
</tr>
<tr>
<td valign="top">===</td>
<td valign="top">is exactly equal to (value and type)</td>
<td valign="top">x===5 is true<br />
x==="5" is false</td>
</tr>
<tr>
<td valign="top">!=</td>
<td valign="top">is not equal
</td>
<td valign="top">x!=8 is true
</td>
</tr>
<tr>
<td valign="top">></td>
<td valign="top">is greater than
</td>
<td valign="top">x>8 is false
</td>
</tr>
<tr>
<td valign="top"><</td>
<td valign="top">is less than
</td>
<td valign="top">x<8 is true</td>
</tr>
<tr>
<td valign="top">>=</td>
<td valign="top">is greater than or equal to
</td>
<td valign="top">x>=8 is false</td>
</tr>
<tr>
<td valign="top"><=</td>
<td valign="top">is less than or equal to
</td>
<td valign="top">x<=8 is true</td>
</tr>
</table>
<br />
<hr />
<h2>How Can it be Used</h2>
<p>Comparison operators can be used in conditional statements to compare values
and take action depending on the result:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table7">
<tbody>
<tr>
<td>
<pre>if (age<18) document.write("Too young");</pre>
</td>
</tr>
</tbody>
</table>
<p>You will learn more about the use of
conditional statements in the next chapter of this tutorial.</p>
<hr />
<h2>Logical Operators</h2>
<p>Logical operators are used in determine the logic between variables or
values.</p>
<p>Given that <b>x=6 and y=3</b>, the table below explains the logical
operators: </p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table2">
<tr>
<th width="15%" align="left">Operator</th>
<th width="45%" align="left">Description</th>
<th width="40%" align="left">Example</th>
</tr>
<tr>
<td valign="top">&&</td>
<td valign="top">and
</td>
<td valign="top"> (x < 10 && y > 1) is true</td>
</tr>
<tr>
<td valign="top">
||</td>
<td valign="top">or
</td>
<td valign="top"> (x==5 || y==5) is false</td>
</tr>
<tr>
<td valign="top">
!</td>
<td valign="top">not
</td>
<td valign="top"> !(x==y) is true</td>
</tr>
</table>
<br />
<hr />
<h2>Conditional Operator</h2>
<p>JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.</p>
<h3>Syntax</h3>
<table class="ex" cellspacing="0" border="1" width="100%" id="table5">
<tbody>
<tr>
<td>
<pre>variablename=(condition)?value1:value2 </pre>
</td>
</tr>
</tbody>
</table>
<h3>Example</h3>
<table class="ex" cellspacing="0" border="1" width="100%" id="table6">
<tbody>
<tr>
<td>
<pre>greeting=(visitor=="PRES")?"Dear President ":"Dear ";</pre>
</td>
</tr>
</tbody>
</table>
<p>If the variable <b>visitor</b> has the value of "PRES", then the variable <b>
greeting</b> will be assigned the value "Dear
President " else it will be assigned "Dear".</p>
<hr />
<a href="js_operators.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_if_else.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -