Java Reference
In-Depth Information
Assigning Variables with the Value of Other Variables
You've seen that you can assign a variable with a number or string, but can you assign a variable with
the data stored inside another variable? The answer is yes, very easily, and in exactly the same way as
giving a variable a literal value. For example, if you have declared the two variables myVariable and
myOtherVariable and have given the variable myOtherVariable the value 22, like this:
var myVariable;
var myOtherVariable;
myOtherVariable = 22;
then you can use the following line to assign myVariable the same value as myOtherVariable (that is,
22).
myVariable = myOtherVariable;
Try It Out Assigning Variables the Values of Other Variables
Let's look at another example, this time assigning variables the values of other variables.
1. Type the following code into your text editor and save it as ch2_examp2.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”>
<body>
<script language=”JavaScript” type=”text/javascript”>
var string1 = “Hello”;
var string2 = “Goodbye”;
alert(string1);
alert(string2);
string2 = string1;
alert(string1);
alert(string2);
string1 = “Now for something different”;
alert(string1);
alert(string2);
</script>
</body>
</html>
Search WWH ::




Custom Search