Basic requirements of declaring a variable.
Or we can exclusively declare and assign the value by using var
var 2my_var='test' // Wrong , variable can't start with 2 var for='test' // Wrong, for is a reserve word
Local & global variableThis is a good practice to use var to declare a variable. Any variable we declare in our main script will be available as global variable inside the function. So local variables of same name inside a function must be declared if they are to be used separately. Here is an example of variables used inside and outside the function.
Accessing variables of Parent windowWe can read the variables of parent window and display ( or use ) in child window after opening. Here is the code for parent window.
Now the child window code ( variable2.htm)
typeof: Checking the type of variableWe can print the type of variable by using typeof function. Here is an example.
Output of above code is string. Similarly based the type of variable, we will get outputs as
string, number,boolean,object etc.