Skip to main content

Variables

Hi people, this is my third js tutorial which explains about using variables with js. So, let’s move on. Before use variables we must declare variables. For that we have to use var keyword.
Ex:        var first_name,last_name;
     var age=23;
     var country= “Srilanka”;
     
When we create functions in js we use local variables inside function. The specialty of local variable is the value of the local variable will destroy once it exits the function. So, the other thing is you can use global variable in anywhere within the js code.
See the below code.
Figure 1

Did you see? Here first I defined variable var_ as a global variable. Then within the variableChecking() ,I declared var_  again as a local variable. So, within the function it is a local var. But in outside the function it is a global variable. You can clearly understand it by run previous program. See the Figure 2 and Figure 3


                                                                        Figure 2
                                                                       Figure 3

Here is another example. This shows some simple operations with integer values.
Figure 4
See how the result variable change, when we do adding and subtracting. Also we can perform x/y (divide) and x*y(multiply)too.
                                                                                          
As same as we can do comparison operations to perform Boolean results like equal (==), not equal (!=), greater than (>), less than(<) and etc.It is like this. If we use same variable as above …


result = (x > y);

document.write(result);


can you guess what is the result?
Think a moment ..
Yeh it is false. Because x is less than y.
OK that all about variable in JS. Actually that is not true. There are lots of things. But this is the idea of variables. You know that how we declare a variable and what are the variable types. So we can use them in everywhere. I hope you will learn them while using them in future tutorials Also I suggest you to try with different example.
Thankyou.

Comments