<< Chapter < Page | Chapter >> Page > |
What is a variable?
You can think of a variable as the symbolic name for a pigeonhole in memory where the script can store a value. The script can change the values stored inthat pigeonhole during the execution of the script. Once a value has been stored in a variable, that value can be accessed by calling out the name of thevariable.
Variable names
Variable names must conform to the naming rules for identifiers. A JavaScript identifier must start with a letter or underscore "_". Following this, you can use either letters or the symbols for the digits (0-9)in the variable name.
JavaScript is case sensitive. Therefore letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Declaring a variable
In many languages, including JavaScript, you must declare a variable before you can use it. However, JavaScript is very loose in this regard. There are two ways to declare a variable in JavaScript:
Scope
When working with variables in JavaScript and other languages as well, you must always be concerned about an issue known as scope. Among other things, scope determines which statements in a script have access to a variable.
Two kinds of variables
JavaScript recognizes two kinds of variables:
Local variables are variables that are declared inside a function. Global variables are variables that are declared outside a function.
Scope
Local variables may only be accessed by other code within the same function following the declaration of the variable.Hence, the scope of local variables is limited to the function or method in which it is declared.
Global variables may be accessed by any code within the script following the declaration of the variable. Hence, the scope of global variables is the entire script.
Use of the keyword var
Using var to declare a global variable is optional. However, you must use var to declare a variable inside a function (a local variable).
A sample script that uses variables
A sample script that uses variables to store data is shown in Listing 4 .
Listing 4 . A sample script that uses variables. |
---|
<!-- File JavaScript04.html --><html><body><script language="JavaScript1.3">//Define the function named getArea()
function getArea(theRadius) {//declare a local variable
var theArea//calculate the area
theArea = Math.PI * theRadius * theRadiusreturn theArea
}//end function getArea()//=========================================//
//declare a global variable without keyword vararea = getArea(3.2)//call the function
document.write("Area is: " + area)</script></body></html> |
The area of a circle
Listing 4 begins by defining a function that will compute and return the area of a circle given the radius of the circle as an incoming parameter.
The code in the function begins by declaring a variable named theArea . Effectively, this declaration sets aside a pigeon hole in memory and gives it the name theArea . Once the variable is declared, it can be accessed by calling out its name. It can be used to store data, or it can be used to retrievedata previously stored there.
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?