1

I am learning javascript by myself.Today I came across with a code example which is quite confusing.

function fool(a, b){

    if(b)
      var c = "Mary"
    alert(c);
}

fool(1, true); //Returns "Mary"
fool(1, false); //Returns undefined instead of error

On the other hand if I do this

function fool(a, b){

    //if(b)
    //  var c = "Mary"
    alert(c);
}

fool(1, true); //Firebug error: ReferenceError: c is not defined
fool(1, false);

An error is occurring on the first chance. How is so ?

1

5 Answers 5

2

Citing MDN

var hoisting

Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.

Example:

bla = 2
var bla;
// ...

// is implicitly understood as:

var bla;
bla = 2;

For your particular case:

// Your code
function fool(a, b){
    if (b)
      var c = "Mary"
    alert(c);
}

// Equivalent code
function fool(a, b){
    var c = undefined;
    if (b)
        c = "Mary";

    alert(c);
}

As a side note, Javascript evolves.

The let keyword was introduced, which works like you expect, but is not supported in all browsers yet, so you can only use it when you control the javascript VM (like using NodeJS, nw.js, ...)

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

Comments

1

This is how the JS engine will rearrange your first example:

function fool(a, b){
    // statement always executed, no matter what
    var c;

    if (b)
       c = "Mary"

    alert(c);
}

Regardless of whether the if statement gets executed or not the var c statement gets hoisted to the top of the function body and therefore be declared in the current function scope.

1 Comment

always use ccuryl brackets!
0

You have commented the variable 'c' and trying to display it.It will give you an error, since there is no variable defined as 'c'.

There is a typo in code.

try this

if(b){
   alert(c);
}

1 Comment

But there is also no declaration for first code and 'fool(1, false);' calling. As 'b' is false it wont enter into the if block.
0

There are two things which i noticed in code 1) If scope: When you dont use bracket({}) it will take only fist sentence under If block. Because else interpreter will not got to know till when if condition is. So yours code which is

 if(b)
      var c = "Mary"
 alert(c);

Should be like

if(b){
      var c = "Mary"
    alert(c);
}

if you want alert(c) executed only on true of if condition.

2) Hoisting: Now second question is even if yours condition is false and sentence doesn't execute how come it alert undefined the reason is variable hoisting which is variable declaration executed before any code execution. So even if you do code like

var k="something";

it will first declare k variable like

var k=undefined;
k="something";

So in yours code first c is declared then execution of code is happening .

I hope it will help

Comments

0
function fool(a, b){

    if(b)
      var c = "Mary"
    alert(c);
}

fool(1, true); //Returns "Mary"
fool(1, false); //Returns undefined instead of error

Let's break down -

You are invoking a function which is passing b as false it goes to the if loop and consider only 1st line as part of if and continue to execute next line which is alert(c). As b='false' so it won't assign Marry to the c var. So, now you are alerting c which is undefined as if won't process it until it get b as true in function parameter.

2 Comments

If anything is not defined.. It should throw Reference Error
Because c is defined, but not executed?

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.