Javascript Tutorial – 4 – More on Variables
To download this you tube video, Click Here
Tutorial More on Variables.
Welcome to the fourth JavaScript tutorial.In this tutorial I’m going to be teaching you how to change variables and use variables in equations. So lets go ahead and start out with variables. Remember to declare a variable you type the variable and then type whatever you want, Say name and set the value equal to Tony
var name=”Tony”;
Whenever you want an output on screen you can use an alert syntax.
var name=”Tony”;
alert(name);
And what if you typed is wrong and you decided that you want to have a new value for the variable name. Well since you have already declared it you don’t need to type the var again since you already did it. You just have to type name and is equal to whatever else you want, Say name equal to Bob
name=”bob”;
Now since JavaScript is reading top to bottom, we got a variable name and is equal to tony. Now we are going to spit that out.’
Say the var name should be changed to name equal to Bob.
So from the step of name equal to Bob the alert will display the value of the name Bob.
alert(name);
After this function you will first get the output as tony and then clicking the OK button you will get the output value as Bob.
Ok now let me teach you one more thing in this tutorial that you can also use variables in variables.
Say we have variable name num1 which stands for number1 which is equal to 30 then and we have an another variable named num2 and say is equal to 80. We can actually include variables in a variable. So I will make a variable called answer and I will set answer equals the variable 1 plus the variable 2. So now the answer has the value of 110. you can view it by making a alert box and you can alert the answer.
var num1=30;
var num2=80;
var answer=(num1+num2);
alert(answer);
You can change any of the two variables numbers and check out. Say if you change the num1 to 330 it automatically changes the answer to 410. Now you can see that if we change the value of the one variable it will change the whole instance.
This is just to show you that how you can use variables in equations of another variables.
Now try to make this, clear things up later on and in the next tutorial we will be taking about how you can actually get information from the user and is lot more useful than typing numbers. In next section we going to learn the first kind of useful thing you can do in JavaScript.