I like to think of variables as containers on a container ship. You can put all different types of content into the containers, move them to another port, empty them, and then replace the container with new content. However, the container ship analogy suffers when you realize that the content in the containers must have magical properties. If you have a container full of numbers and you add a string, the whole container magically becomes a string. Because JavaScript is untyped (or weakly typed), both the contents and the characteristics of the variable can change. The main point, though, for readers new to the concept of a variable is that variables are containers with changeable contents. Declaring and NamingJavaScript, like most scripting languages, has two basic ways of declaring a variable. Variables are declared using the var word. You simply type in var followed by a variable name and value. The following are typical examples:
By taking each variable one at a time, you can see how the different data types discussed are placed into a variable:
Declaring a variable alerts the computer to the fact that a new variable is available to use. After a variable is declared, it need not be declared again. For example, in loop structures, the counter variable can be defined in the initialize section, but not in the test or change (increment/decrement) sections. For example, the following code segment shows that the variable named counter is declared in the first segment but then is not declared again: for (var counter=0; counter < 40; counter++) {....
Some programmers like to initialize all of their variables at the beginning of a script with undefined values. Then later they can use them without having to remember to add var. Also, you can have a single line with several variable definitions, with each variable separated by a comma or a semicolon, as the following script illustrates. clutter.html<html> <head> <script language="JavaScript"> var a=20; b=30, c="wacka wacka do"; gap=" "; document.write(a+ gap + b + gap +c); </script> <body bgcolor=#C0FFEE> </body> </html> I generally avoid declaring more than a single variable on a line. Multiple declarations in a line, while workable, can clutter what has been defined and what a variable has been defined as. The script clutter.html amply illustrates such confusion. (By the way, the character following the c in the bgcolor value is a zero, [0], not a capital O.) You may omit the var keyword in your variable declarations, and you undoubtedly will see scripts in which the programmers have done so. For example, the following are perfectly good examples of such declarations: acme = "The Best"; cost = 23.22; While JavaScript accepts these declarations for global variables, you can run into problems elsewhere by omitting var. (See the note in the following section.) Thus, for a good programming habit that will avoid problems, always use the var keyword when declaring a variable. Global and Local VariablesVariables in JavaScript have scope. The scope refers to the regions of the script where the variables can be used. A global variable, as the name implies, has global scope and is defined in the entire script. Local variables are local to the functions in which they are defined. As a general rule, avoid naming any two variables, whether local or global, with the same name or identifier. NOTE While using the keyword var is optional in declaring global variables, problems can arise if you do not incorporate var in defining your local variables. When using var in a local variable declaration, the program recognizes it as a local variable, not a change in the value of a global variable. With no var keyword used, your script cannot tell the difference, and you risk inadvertently changing the value of a global variable. The moral to this story is to always use the var keyword for variable declaration. Within a function, a local variable has precedence over a global variable of the same name. So, if your global variable named ID has the value Fred, and a function also with a variable named ID has a value of Ethel, the name Ethel will appear when the function displays the variable's value. However, if you display the value of the ID variable from outside the function, the value will be Fred. The following script uses four variables to demonstrate these differences. Two global variables are defined, and then two local variables are defined within a function. One of the global and local variables share a common identifier, localGlobal. When fired from the function, the local variable's value is displayed; when displayed from the global script, the global variable's value is displayed. GlobalLocal.html<html>
<head>
<script language="JavaScript">
var onlyGlobal="This variable is only global!";
var localGlobal ="I\'m global now!";
function showMe( ) {
var localGlobal="I\'m now local";
var onlyLocal="Only works on the local level."
alert(localGlobal + " -- " + onlyLocal);
}
showMe( );
document.write(onlyGlobal + "<p>"+ localGlobal);
alert(onlyGlobal);
</script>
<body bgcolor=#CadDad>
</body>
</html>
|
|||||||||||||
Disclaimer
1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us or use the "Report this article" button on this page to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. link to this article |
|||||||||||||