Java Reference
In-Depth Information
You put data into your variables, a process called assigning values to your variables, by using the equals
sign (=). For example, if you want your variable named myFirstVariable to hold the number 101, you
would write this:
myFirstVariable = 101;
The equals sign has a special name when used to assign values to a variable; it's called the assignment
operator .
Try It Out Declaring Variables
Let's look at an example in which a variable is declared, store some data in it, and fi nally access its con-
tents. You'll also see that variables can hold any type of data, and that the type of data being held can be
changed. For example, you can start by storing text and then change to storing numbers without JavaScript
having any problems. Type the following code into your text editor and save it as ch2_examp1.htm:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
</head>
<body>
<script type=”text/javascript”>
var myFirstVariable;
myFirstVariable = “Hello”;
alert(myFirstVariable);
myFirstVariable = 54321;
alert(myFirstVariable);
</script>
</body>
</html>
As soon as you load this into your web browser, it should show an alert box with “Hello” in it, as
shown in Figure 2-1. This is the content of the variable myFirstVariable at that point in the code.
Figure 2-1
Search WWH ::




Custom Search