HTML and CSS Reference
In-Depth Information
<script type=”text/javascript”>
var message = “My message”;
document.write(message);
</script>
In that example, I created a variable called message and then passed its value as an argu-
ment to document.write() . You can also assign the results of an expression to a vari-
able:
var sum = 5 + 5;
And you can use variables in your expressions:
var firstName = “George”;
var lastName = “Washington”;
var name = firstName + “ “ + lastName;
Let's break down a variable declaration into pieces. Here's a declaration:
var message = “My message”;
The line begins with var , which indicates that this is a variable declaration. The name of
this variable is message . There are a number of rules that apply to naming variables. I list
them shortly. The assignment operator (=) is used to assign a value to the variable when
it's declared. The value on the right side of the operator is assigned to the newly declared
variable.
Variable names must conform to the following rules:
Variable names can include only letters, numbers, and the underscore (_) or dollar
sign ($) character.
n
Variable names cannot start with a number.
n
You cannot use any reserved words as a variable name. Reserved words are words
that have a specific meaning for the JavaScript interpreter. For example, naming a
variable named var won't work. Table 14.2 contains a full list of JavaScript
reserved words.
n
As a matter of style, JavaScript variables begin with a lowercase letter. If a variable
name contains multiple words, usually an underscore is used to join the two
words, or the first letter of the second word is uppercase. So you would write
my_variable or myVariable .
n
14
 
Search WWH ::




Custom Search