📄 js_variables.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 Variables</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 Variables</h1>
<a href="js_comments.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_operators.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">Variables are "containers"
for storing information:</p>
<p class="intro">x=5;
length=66.10;</p>
<hr />
<h2>Do You Remember Algebra From School?</h2>
<p>Hopefully you remember algebra like this from school: x=5, y=6, z=x+y.</p>
<p>Do you remember that a letter (like x) could be used to hold a value (like
5), and that you could use the information above calculate the value of z to be
11?</p>
<p>Sure you do!</p>
<p>These letters are called <b>variables</b>, and variables can be used to hold values
(x=5) or expressions (z=x+y).</p>
<hr />
<h2>JavaScript Variables</h2>
<p>As with algebra, JavaScript variables are used to hold values or expressions.</p>
<p>A variable can have a short name, like <b>x</b>, or a more describing name
like <b>length</b>. </p>
<p>A JavaScript variable can also hold a text value like in <b>carname="Volvo"</b>.</p>
<p>Rules for JavaScript variable names:</p>
<ul>
<li>Variable names are case sensitive (<b>y</b> and <b>Y</b> are two different
variables)</li>
<li>Variable names must <b>begin with a letter</b> or the underscore character</li>
</ul>
<p><b>NOTE:</b> Because JavaScript is case-sensitive, variable names are
case-sensitive.</p>
<hr />
<h2>Example</h2>
<p>A
variable's value can change during the execution of a script. You can refer to a variable by
its name to display or change its value.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_variable">This example will
show you how</a></p>
<hr />
<h2>Declaring (Creating) JavaScript Variables</h2>
<p>Creating variables in JavaScript is most often referred to as "declaring"
variables.</p>
<p>You can declare JavaScript variables with the <b>var statement</b>:</p>
<table class="ex" cellspacing="0" border="1" width="100%"><tr><td>
<pre>var x;
var carname;</pre>
</td></tr></table>
<p>After the declaration shown above, the variables has no values, but you can assign values to the variables while you declare them:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre>var x=5;
var carname="Volvo";</pre>
</td>
</tr>
</table>
<p><b>Note:</b> When you assign a text value to a variable, you use quotes
around the value.</p>
<hr />
<h2>Assigning Values to JavaScript Variables</h2>
<p>You assign values to JavaScript variables with <b>assignment statements</b>:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre>x=5;
carname="Volvo";</pre>
</td>
</tr>
</table>
<p>The variable name is on the left side of the = sign, and the value you
want to assign to the variable is on the right.</p>
<p>After the execution of the statements above, the variable <b>x</b> will hold
the value <b>5</b>, and <b>carname</b> will hold the value <b>Volvo</b>.</p>
<hr />
<h2>Assigning Values to Undeclared JavaScript Variables</h2>
<p>If you assign values to variables that has not yet been declared,
the variables will automatically be declared.</p>
<p>These statements:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table1">
<tr>
<td>
<pre>x=5;
carname="Volvo";</pre>
</td>
</tr>
</table>
<p>have the same effect as:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table2">
<tr>
<td>
<pre>var x=5;
var carname="Volvo";</pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>Redeclaring JavaScript Variables</h2>
<p>If you redeclare a JavaScript variable, it will not lose its original value.</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table4">
<tr>
<td>
<pre>var x=5;
var x;</pre>
</td>
</tr>
</table>
<p>After the execution of the statements above, the variable x will still have
the value of 5. The value of x is not reset (or cleared) when you redeclare it.</p>
<hr />
<h2>JavaScript Arithmetic</h2>
<p>As with algebra, you can do arithmetic with JavaScript variables:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
<tr>
<td>
<pre>y=x-5;
z=y+5;</pre>
</td>
</tr>
</table>
<p>You will learn more about the operators that can be used between JavaScript
variables in the next chapter of this tutorial.</p>
<hr />
<a href="js_comments.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_operators.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 + -