We will be using each of these values in this course. Those that are not clear now will hopefully become clear as we use them.
Once the variable is declared, you can set it to any one of the values shown in the above table. Here is an example:
myVariable = 5
Later on in the program you can change its value to something different:
myVariable = 33
You can even change its type by simply assigning it a different type of value. For instance our variable was originally assigned a number. We can make it a string later in the program by simply assigning it one as follows:
myVariable = "this is a string"
Note that the variables in some other languages, such a C, are strongly typed and you are not allowed to randomly change the variable's type as we did here.
This is method that you will probably use most of the time to declare and initialize your variables.
myVariable = "this is not too cool"
I personally do not use the last technique because I think the others make it clear that you are defining a variable. Also, as you will learn later, this technique makes the variable available throughout your code which may not be what you want.
Lets try some of these variables in our Lab. Type in the following code in the area that is provided in the BODY of our lab. Remember that the <SCRIPT> tags are already provided for you. All you have to do is type in the two lines shown in the lighter blue.
Also note the // prior to the words string, number, Boolean, Null, and object. Anything on the line that appears after the // is a comment and is ignored when the browser interprets the rest of the script.
The last variable we initialized is an object type. We will start our discussion of objects in lesson 7.
To save you some typing, I put the above script in the electronic chalkboard below so you could test it. When you press Test, the results are displayed for each change of the variable with no error. Using the electronic chalk board, you can change any of the variables to a different type value to see for yourself the results.
Declare and initialize two variables, x and y, with the values 12 and 27, respectively. Declare a third value z and set it equal to the sum of these two variables. Now use document.write(z) to display the results. When you press Test, you should of course get 39 displayed in the top frame.
Compare what you have so far to my solution to step 1.
Now that you have that working we are going to add to it. Under the first two variables, declare and initialize two more variables xStr and yStr with the values "<BR>This is " and "fun!" respectively. Declare and set a third variable zStr equal to xStr + yStr. Now type document.write(zStr) under the line document.write(z). When you press Test you should get the following:
Compare what you have so far to my solution to step 2.
We have demonstrated that if you use the + operator between two
variables (or values) that are numbers then you will add them. If you use
the + operator between two strings you will make it into one string. Now
lets make one more change to our script and see what happens when we
combine a string with a number. Lets change the line document.write(z)
Take a close look at what we included in the document write. There are three strings ("The sum of ", " + " and " = ") and there are three variables (x, y, z) that contain numbers. When you combine numbers with a string, the result is a string.
Take a look at my solution to this lab exercise and see if your final version agrees.
Try changing the values for the variables x and y to something different and pressing test to see the results. Doing this should have given you some feel for the advantage of using variables.
There is no additional assignments with this lesson. Please read over the lesson again and make sure your understand it all before proceeding. Also, make sure you understand the exercises that we did in our Lab and Electronic Chalkboard. Don't forget that we also have a Library available that can be used to learn more about the concepts presented in each lesson.
[ Top of Page | Contents ]