Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information given to calculate the value of z to be 11?
These letters are called variables. Variables can be used to hold values (x = 5) or expressions (z = x + y).
JavaScript Variables
As with algebra, JavaScript variables are used to hold values or expressions. A variable can have a short name, like x, or a more descriptive name, like carname.
Rules for JavaScript variable names:
- Variable names are case sensitive (y and Y are two different variables).
- Variable names must begin with a letter, the underscore character, or a dollar sign. (The $ character is used primarily by code-generation tools.)
- Subsequent characters may be letter, number, underscore, or dollar sign.
- There are 59 reserved words that are not legal variable names.
- Because JavaScript is case sensitive, variable names are case sensitive.
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.
<html><body><script type="text/javascript">var firstname;firstname="Hege";document.write(firstname);document.write("<br />");firstname="Tove";document.write(firstname);</script><p>The script above declares a variable,assigns a value to it, displays the value, changes thevalue,and displays the value again.</p></body></html>
Declaring (Creating) JavaScript Variables
Creating variables in JavaScript is most often referred to as “declaring” variables.
You can declare JavaScript variables with the var statement:
var x;var carname;
After the declaration shown, the variables are empty. (They have no values yet.) However, you can also assign values to the variables when you declare them:
var x=5;var carname="Volvo";
After the execution of the preceding statements, the variable x will hold the value 5, and carname will hold the value Volvo.
When you assign a text value to a variable, use quotes around the value.
Assigning Values to Undeclared JavaScript Variables
If you assign values to variables that have not yet been declared, the variables will automatically be declared.
The following statements:
x=5;carname="Volvo";
have the same effect as these:
var x=5;var carname="Volvo";
Redeclaring JavaScript Variables
If you redeclare a JavaScript variable, it will not lose its original value.
var x=5;var x;
After the execution of the preceding statements, the variable x will still have the value of 5. The value of x is not reset (or cleared) when you redeclare it.
JavaScript Arithmetic
As with algebra, you can do arithmetic operations with JavaScript variables:
y=x-5;z=y+5;
Sometimes the results seem unpredictable. If at least one variable on the right side of an assignment expression contains a string value, the result will be a string and the “+” operator is applied as the concatenation operator to the toString() values of the variables. Only if all the variables to the right of the assignment operator are numbers will the result be a number.
0 comments:
Post a Comment