📄 js_operators.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 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 Operators</h1>
<a href="js_variables.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_comparisons.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">The operator = is used to assign values.</p>
<p class="intro">The operator + is used to add values.</p>
<hr />
<p>The assignment operator <b>=</b> is used to assign values to JavaScript
variables.</p>
<p>The arithmetic operator + is used to add values together.</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table4"><tr><td>
<pre>y=5;
z=2;
x=y+z;</pre>
</td></tr></table>
<p>The value of x, after the execution of the statements above is 7.</p>
<hr />
<h2>JavaScript Arithmetic Operators</h2>
<p>Arithmetic operators are used to perform arithmetic between variables and/or
values.</p>
<p>Given that <b>y=5</b>, the table below explains the arithmetic operators: </p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<th width="15%" align="left">Operator</th>
<th width="40%" align="left">Description</th>
<th width="25%" align="left">Example</th>
<th width="20%" align="left">Result</th>
</tr>
<tr>
<td valign="top">+</td>
<td valign="top">Addition
</td>
<td valign="top">x=y+2
</td>
<td valign="top">x=7
</td>
</tr>
<tr>
<td valign="top">-</td>
<td valign="top">Subtraction
</td>
<td valign="top">x=y-2</td>
<td valign="top">x=3</td>
</tr>
<tr>
<td valign="top">*</td>
<td valign="top">Multiplication
</td>
<td valign="top">x=y*2</td>
<td valign="top">x=10</td>
</tr>
<tr>
<td valign="top">/</td>
<td valign="top">Division
</td>
<td valign="top">x=y/2</td>
<td valign="top">x=2.5</td>
</tr>
<tr>
<td valign="top">%</td>
<td valign="top">Modulus (division remainder)
</td>
<td valign="top">x=y%2</td>
<td valign="top">x=1</td>
</tr>
<tr>
<td valign="top">++</td>
<td valign="top">Increment</td>
<td valign="top">x=++y</td>
<td valign="top">x=6
</td>
</tr>
<tr>
<td valign="top">--</td>
<td valign="top">Decrement</td>
<td valign="top">x=--y</td>
<td valign="top">x=4
</td>
</tr>
</table>
<br />
<hr />
<h2>JavaScript Assignment Operators</h2>
<p>Assignment operators are used to assign values to JavaScript
variables.</p>
<p>Given that <b>x=10</b> and <b>y=5</b>, the table below explains the assignment operators:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<th width="15%" align="left">Operator</th>
<th width="40%" align="left">Example</th>
<th width="25%" align="left">Same As</th>
<th width="20%" align="left">Result</th></tr>
<tr>
<td valign="top">=</td>
<td valign="top">x=y</td>
<td valign="top"> </td>
<td valign="top">x=5</td>
</tr>
<tr>
<td valign="top">+=</td>
<td valign="top">x+=y</td>
<td valign="top">x=x+y</td>
<td valign="top">x=15</td>
</tr>
<tr>
<td valign="top">-=</td>
<td valign="top">x-=y</td>
<td valign="top">x=x-y</td>
<td valign="top">x=5</td>
</tr>
<tr>
<td valign="top">*=</td>
<td valign="top">x*=y</td>
<td valign="top">x=x*y</td>
<td valign="top">x=50</td>
</tr>
<tr>
<td valign="top">/=</td>
<td valign="top">x/=y</td>
<td valign="top">x=x/y</td>
<td valign="top">x=2</td>
</tr>
<tr>
<td valign="top">%=</td>
<td valign="top">x%=y</td>
<td valign="top">x=x%y</td>
<td valign="top">x=0</td>
</tr>
</table>
<br />
<hr />
<h2>The + Operator Used on Strings</h2>
<p>The + operator can also be used to add string variables or text values
together.</p>
<p>To add
two or more string variables together, use the + operator.</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tbody>
<tr>
<td>
<pre>txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;</pre>
</td>
</tr>
</tbody>
</table>
<p>After the execution of the statements above, the variable txt3 contains "What a verynice day".</p>
<p>To add a space between the two strings, insert a space into one of the
strings:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tbody>
<tr>
<td>
<pre>txt1="What a very ";
txt2="nice day";
txt3=txt1+txt2;</pre>
</td>
</tr>
</tbody>
</table>
<p>or insert a space into the expression:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
<tbody>
<tr>
<td>
<pre>txt1="What a very";
txt2="nice day";
txt3=txt1+" "+txt2;</pre>
</td>
</tr>
</tbody>
</table>
<p>After the execution of the statements above, the variable txt3 contains:</p>
<p>"What a very nice day"</p>
<hr />
<h2>Adding Strings and Numbers</h2>
<p>Look at these examples:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table5"><tr><td><pre>
x=5+5;
document.write(x);
x="5"+"5";
document.write(x);
x=5+"5";
document.write(x);
x="5"+5;
document.write(x);
</pre></td></tr></table>
<p><a target="_blank" href="tryit.asp@filename=tryjs_variables">Try it yourself</a>.</p>
<p>The rule is:</p>
<p><b>If you add a number and a string, the result will be a string.</b></p>
<hr />
<a href="js_variables.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_comparisons.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 + -