0

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

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you .... You mean C variable is auto create in global level with in execution... ?
Yes, thats correct, and the value to var c gets assigned if the 'if condition' is satisfied, but if you console.log the var c it will say undefined and not undeclared, that means it has been declared in the global context
1

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

1

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

0

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

0

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

0

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

0

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

0

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 />");
    //-->

4 Comments

Thank you...... Ohh it`s my wrong.... but I asked how can c variable work in global scope.... ?
c variable is on te scope already as all if statements, its conditions and it is code ..all on te scope unless you move it inside a function.
i added some more info above.
or u can call the function and it will automatically add b to the scope. but careful u still have to keep declaring b variable without the var keyword.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.