Why c variable of if condition can work in global scope and b variable not work on global... ?
<script type="text/javascript" language="javascript">
<!--
var a = 45;
function print(){
var b = 10;
document.write(a);
}
if(a == 45)
{
var c = 10;
}
print();
document.write("a is : " + a + "<br />");
document.write("b is : " + b + "<br />");
document.write("c is : " + c + "<br />");
//-->
</script>
8 Answers 8
Why variable b is local
When variable b will always be created when execution context of function print() is created and will be destroyed once the function is completed. So it is dependent on that function, hence is local to that function.
Why variable c is global
But in case of var c inside if condition, its execution context is created at the global level and var c is declared. It is just assigned a value when that condition in 'if' becomes true.
You can read hoisting for more details: https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
2 Comments
Because b variable having block scope within print function, and c variable is declared outside the print function thats why c variable having global scope
Comments
When you declare a variable in a function, it's only available in the function and it will disappear after the function is executed. Declaring a variable in a function makes it only accessible within the function; outer commands cannot access that variable.
Comments
you can declare b in global scope and make it 10 when condition is satisfied. In your code b is inside scope of the function hence it si not global
<script type="text/javascript" language="javascript">
<!--
var a = 45,b;
function print(){
b = 10;
document.write(a);
}
if(a == 45)
{
var c = 10;
}
document.write("a is : " + a + "<br />");
document.write("b is : " + b + "<br />");
document.write("c is : " + c + "<br />");
//-->
</script>
Comments
It's because a == 45 so c = 10, but b is scoped off in your print function. Note that you should avoid document.write and print is a property of window already, so you're overwriting it.
Comments
2 reasons why your code is failing.
1. You are not calling print function
2 You are using local variable in global scope.
Variable b is defined in function so it's local variable, and it's scope is in function it self. So you should declare b to global scope.
Try Initializing var b and removing var keyword from function
var a= 45, b = 0;
function print(){
b = 10;
...
More detailed understanding of Variable Scope
Comments
When variable declared using var it does not belong to the scope of the statements, thus there is not a scope of the if and the c is declared globally.
But if you declare the variable in the function, it belongs to the nearest enclosing function's scope, so b cannot be a global one.
From MDN
Variable declarations, wherever they occur, are processed before any code is executed. The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.
Comments
Simply because b variable is initialized inside the function print.. and c variable is initialised on the global scope. so at this line:
document.write("b is : " + b + "<br />");
you trying to get the value of b variable but ur not calling the function print..
look at this :
var a = 45;
var b;
var c;
function print(){
b = 10;
document.write(a);
}
if(a == 45)
{
var c = 10;
}
document.write("a is : " + a + "<br />");
document.write("b is : " + b + "<br />");
document.write("c is : " + c + "<br />");
//-->
it will print the following:
a is : 45
b is : undefined
c is : 10
because b didn`t change as the function has not yet been called .. but c has changed by the if statement which lies on the global scope.
var a = 45;
var c;
function print() {
b = 10;
document.write(a);
}
print()
if (a == 45) {
var c = 10;
}
document.write("a is : " + a + "<br />");
document.write("b is : " + b + "<br />");
document.write("c is : " + c + "<br />");
//-->