JavaScript variablesBasic requirements of declaring a variable.
Declaring a JavaScript variable.We can simply assign the value to the variable like this
Or we can exclusively declare and assign the value by using var
Examples:
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 a global variable inside the function. So local variables of the 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. Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
|
More on Loops and Variables |