HTML and CSS Reference
In-Depth Information
EXAMPLE 3.6
<html>
<head><title>JavaScript Variables</title>
1
<script type="text/javascript">
2
var first_name="Christian"; // first_name is assigned a value
3
var last_name="Dobbins";
// last_name is assigned a value
4
var age = 8;
5
var ssn ;
// Unassigned variable
6
var job_title=null ;
</script>
7
</head>
8
<body bgcolor="lightgreen">
<big>
9
<script type="text/javascript">
10
document.write("<b>Name:</b> " + first_name + " "
+ last_name + "<br />");
11
document.write("<b>Age:</b> " + age + "<br />");
12
document.write("<b>SSN:</b> " + ssn + "<br />");
13
document.write("<b>Job Title:</b> " + job_title+"<br />");
14
ssn="xxx-xx-xxxx";
15
document.write("<b>Now SSN is:</b> " + ssn , "<br />");
</script>
<p>
<img src="Christian.gif" /></body>
</p>
</big>
</body>
</html>
Output:
10 Name: Christian Dobbins
11 Age: 8
12
SSN: undefined
13
Job Title: null
15
Now Ssn is: xxx-xx-xxx
EXPLANATION
1
This JavaScript program is placed within the document head. Because the head of
the document is processed before the body, this assures you that the variable def-
initions will be defined first.
2
The string “Christian” is assigned to the variable called first_name .
3
The string “Dobbins” is assigned to the variable called last_name .
4
The number 8 is assigned to the variable called age .
5
The variable called ssn is not assigned any value at all. It is an uninitialized vari-
able. The return value is undefined .
 
Search WWH ::




Custom Search