HTML and CSS Reference
In-Depth Information
FORMAT
var variable_name = value; // initialized
var variable_name;
// uninitialized
variable_name;
// wrong
To declare a variable called firstname , you could say
var first_name="Ellie"
or
first_name ="Ellie";
or
var first_name;
You can declare multiple variables on the same line by separating each declaration
with a comma. For example, you could say
var first_name, middle_name, last_name;
EXAMPLE 3.5
<html>
<head><title>Using the var Keyword</title>
<script type="text/javascript">
1
var language="English" ;
// Variable is initialized
2
var name ;
// OK, undefined variable
3
age ;
// Not OK! var keyword missing ERROR!
</script>
</head>
<body bgcolor="silver">
<big>
<script type="text/javascript">
document.write("Language is " + language + "<br />");
document.write("Name is "+ name + "<br />");
4
document.write("Age is "+ age + "<br />");
</script>
</big>
</body>
</html>
EXPLANATION
1
The variable called language is defined and initialized. The var keyword is not re-
quired here, but is recommended.
2
Because the variable called name is not initialized, the var keyword is required here.
Continues
Search WWH ::




Custom Search