Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 118743 of Variables

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Variables
  • Revision title: Variables
  • Revision id: 118743
  • Created:
  • Creator: user01
  • Is current revision? No
  • Comment Replaced note with template; 12 words added, 14 words removed

Revision Content

{{ mergedInto("en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables") }}

Variables

You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the \uXXXX Unicode escape sequences listed on the Unicode Escape Sequences page as characters in identifiers.

Some examples of legal names are Number_hits, temp99, and _name.

Declaring Variables

You can declare a variable in two ways:

  • With the keyword var. For example, var x = 42. This syntax can be used to declare both local and global variables.
  • By simply assigning it a value. For example, x = 42. This always declares a global variable and generates a strict JavaScript warning. You shouldn't use this variant.

Evaluating Variables

A variable declared using the var statement with no initial value specified has the value undefined.

An attempt to access an undeclared variable will result in a ReferenceError exception being thrown:

var a;
print("The value of a is " + a); // prints "The value of a is undefined"
print("The value of b is " + b); // throws ReferenceError exception

You can use undefined to determine whether a variable has a value. In the following code, the variable input is not assigned a value, and the if statement evaluates to true.

var input;
if(input === undefined){
  doThis();
} else {
  doThat();
}

Not sure how the following is related to "Variables" section The undefined value behaves as false when used in a boolean context. For example, the following code executes the function myFunction because the myArray element is not defined:

var myArray = new Array();
if (!myArray[0]) myFunction();

When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in boolean contexts. For example:

var n = null;
print(n * 32); // prints 0

Variable Scope

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within. For example the following code will print 0 instead of throwing if condition is false:

if (condition) {
  var x = 5;
}
print(x ? x : 0);

Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception:

print(x === undefined); // prints "true"
var x = 3;

Global Variables

need links to pages discussing scope chains and the global object Global variables are in fact properties of the global object. In web pages the global object is window, so you can set and access global variables using the window.variable syntax.

Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber is declared in a FRAMESET document, you can refer to this variable from a child frame as parent.phoneNumber.

See Also

Sharp variables in JavaScript

{{ PreviousNext("Core_JavaScript_1.5_Guide:Values", "Core_JavaScript_1.5_Guide:Constants") }}

{{ languages( { "zh-tw": "zh_tw/Core_JavaScript_1.5_教學/變數", "es": "es/Gu\u00eda_JavaScript_1.5/Variables", "fr": "fr/Guide_JavaScript_1.5/Variables", "ja": "ja/Core_JavaScript_1.5_Guide/Variables", "ko": "ko/Core_JavaScript_1.5_Guide/Variables", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Zmienne", "zh-cn": "cn/Core_JavaScript_1.5_Guide/\u53d8\u91cf" } ) }}

Revision Source

<p>{{ mergedInto("en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables") }}</p>
<h3 name="Variables">Variables</h3>
<p>You use variables as symbolic names for values in your application. The names of variables, called <em>identifiers</em>, conform to certain rules.</p>
<p>A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).</p>
<p>Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the \uXXXX Unicode escape sequences listed on the <a href="/en/Core_JavaScript_1.5_Guide/Unicode#Unicode_Escape_Sequences" title="en/Core_JavaScript_1.5_Guide/Unicode#Unicode_Escape_Sequences">Unicode Escape Sequences</a> page as characters in identifiers.</p>
<p>Some examples of legal names are <code>Number_hits</code>, <code>temp99</code>, and <code>_name</code>.</p>
<h4 name="Declaring_Variables">Declaring Variables</h4>
<p>You can declare a variable in two ways:</p>
<ul> <li>With the keyword <a href="/en/Core_JavaScript_1.5_Reference/Statements/var" title="en/Core_JavaScript_1.5_Reference/Statements/var">var</a>. For example, <code>var x = 42</code>. This syntax can be used to declare both <a href="#Variable_Scope">local and global</a> variables.</li> <li>By simply assigning it a value. For example, <code>x = 42</code>. This always declares a <a href="#Global_Variables">global variable</a> and generates a strict JavaScript warning. You shouldn't use this variant.</li>
</ul>
<h4 name="Evaluating_Variables">Evaluating Variables</h4>
<p>A variable declared using the <code>var</code> statement with no initial value specified has the value <a href="/en/Core_JavaScript_1.5_Reference/Global_Properties/undefined" title="en/Core_JavaScript_1.5_Reference/Global_Properties/undefined">undefined</a>.</p>
<p>An attempt to access an undeclared variable will result in a ReferenceError exception being thrown:</p>
<pre class="eval">var a;
print("The value of a is " + a); // prints "The value of a is undefined"
print("The value of b is " + b); // throws ReferenceError exception
</pre>
<p>You can use <code>undefined</code> to determine whether a variable has a value. In the following code, the variable <code>input</code> is not assigned a value, and the <code><a href="/en/Core_JavaScript_1.5_Reference/Statements/if...else" title="en/Core_JavaScript_1.5_Reference/Statements/if...else">if</a></code> statement evaluates to <code>true</code>.</p>
<pre class="eval">var input;
if(input === undefined){
  doThis();
} else {
  doThat();
}
</pre>
<p><span class="comment">Not sure how the following is related to "Variables" section</span> The <code>undefined</code> value behaves as <code>false</code> when used in a boolean context. For example, the following code executes the function <code>myFunction</code> because the <code>myArray</code> element is not defined:</p>
<pre class="eval">var myArray = new Array();
if (!myArray[0]) myFunction();
</pre>
<p>When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in boolean contexts. For example:</p>
<pre class="eval">var n = null;
print(n * 32); // prints 0
</pre>
<h4 name="Variable_Scope">Variable Scope</h4>
<p>When you declare a variable outside of any function, it is called a <em>global</em> variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a <em>local</em> variable, because it is available only within that function.</p>
<p>JavaScript does not have <a href="/en/Core_JavaScript_1.5_Guide/Block_Statement#Block_Statement" title="en/Core_JavaScript_1.5_Guide/Block_Statement#Block_Statement">block statement</a> scope; rather, it will be local to the code that the block resides within. For example the following code will print <code>0</code> instead of throwing if <code>condition</code> is <code>false</code>:</p>
<pre class="eval">if (condition) {
  var x = 5;
}
print(x ? x : 0);
</pre>
<p>Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception:</p>
<pre class="eval">print(x === undefined); // prints "true"
var x = 3;
</pre>
<h4 name="Global_Variables">Global Variables</h4>
<p><span class="comment">need links to pages discussing scope chains and the global object</span> Global variables are in fact properties of the <em>global object</em>. In web pages the global object is <a href="/en/DOM/window" title="en/DOM/window">window</a>, so you can set and access global variables using the <code>window.<em>variable</em></code> syntax.</p>
<p>Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called <code>phoneNumber</code> is declared in a <code>FRAMESET</code> document, you can refer to this variable from a child frame as <code>parent.phoneNumber</code>.</p>
<h4 name="See_Also">See Also</h4>
<p><a href="/en/Sharp_variables_in_JavaScript" title="en/Sharp_variables_in_JavaScript">Sharp variables in JavaScript</a></p>
<p>{{ PreviousNext("Core_JavaScript_1.5_Guide:Values", "Core_JavaScript_1.5_Guide:Constants") }}</p>
<p>{{ languages( { "zh-tw": "zh_tw/Core_JavaScript_1.5_教學/變數", "es": "es/Gu\u00eda_JavaScript_1.5/Variables", "fr": "fr/Guide_JavaScript_1.5/Variables", "ja": "ja/Core_JavaScript_1.5_Guide/Variables", "ko": "ko/Core_JavaScript_1.5_Guide/Variables", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Zmienne", "zh-cn": "cn/Core_JavaScript_1.5_Guide/\u53d8\u91cf" } ) }}</p>
Revert to this revision