Understanding JavaScript hoisting and truthy & falsy
Asked Answered
E

2

6

I've been reading about JavaScript hoisting sometime back.

JavaScript Scoping and Hoisting by Ben Cherry
Two words about “hoisting” by Dmitry Soshnikov

and, some more about JavaScript type-coercion, truth & false test: Truth, Equality and JavaScript and some other resource

And while practicing some, and found I m missing some important concept about the hoisting and a variable' truthy & falsy.

1: 'if' truth test with duplicate variable declaration

var foo = 1; 
function bar() { 
    if (!foo) { 
    alert('inside if');
        var foo = 10; 
    } 

} 
bar();

o/p: inside if

Doubt: 'foo' value being '1', if(!foo) should evaluates to false and that block should not be executed (quoting from above resources: hoisting affects only the var & function declaration, but not the execution). But why is that alert is shown. This is not the case if I directly use false (shown in the below no-tricks code: snippet #3)

2: 'if' truth test without duplicate variable declaration

var foo = 1; 
function bar() { 
    if (!foo) { 
        alert('inside if');
    } 

} 
bar();

o/p: no output; means control not entered 'if' block
This is what one could expect

3: 'if' using 'false' with duplicate variable declaration

var foo = 1; 
function bar() { 
    if (false) { 
        alert('inside if');
        var foo = 10; 
    } 
} 
bar();

o/p: no output; means control not entered 'if' block
This is what one could expect

Someone please clarify. Thanks

Eugenie answered 20/7, 2011 at 17:37 Comment(0)
H
9

For your example number 1, the alert is shown because you're using var inside the function and the var declaration is hoisted to the top of the function, so it is equivalent to:


var foo = 1; 
function bar() {
    var foo;
    if (!foo) {
        alert('inside if');
        foo = 10; 
    } 

} 
bar();

One might conclude that these sorts of issues offer compelling reason to declare all variables explicitly at the top of the function.

Halcomb answered 20/7, 2011 at 17:43 Comment(2)
My bad. I didn't mind the function at all. I m only concentrating on the type coercion and hoisting (they are revolving in my head). If the duplicate declaration is not inside a function, then 'foo' will have the previous value (1) even after the second declaration. This all thinking made me to miss the function used. Thanks for opening my eye :)Eugenie
Why doesen't number three work then? if a variable is undefined doesent it mean its false?Spillar
I
1

Only variable declaration is eagerly evaluated. The variable assignment in your first case (in the if block) occurs only upon entering the if block.

The variable you only declare, but not assign any value to, has the value of undefined (which coerces to false).

Into answered 20/7, 2011 at 17:42 Comment(1)
Thanks for the answer. @patrick dw and You both answered it correct (in fact thanks for ignoring my ignorance). But I can only accept only one answer and as Mr. Patrick answered first I've accepted his. By the way I've 'Up'ed your answer too. Thanks.Eugenie

© 2022 - 2024 — McMap. All rights reserved.