Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding JavaScript hoisting and truthy & falsy

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

like image 974
manikanta Avatar asked Jul 20 '11 17:07

manikanta


People also ask

How do you explain hoisting in JavaScript?

In JavaScript, Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution. Basically, it gives us an advantage that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.

What is hoisting and closure in JavaScript?

Specifically, we discussed how hoisting allows you to refer to JavaScript variables and functions prior to their declaration, and how closures allow you to access function-scoped variables outside of that specific function.

Does JavaScript hoist let and const?

The let and const KeywordsVariables defined with let and const are hoisted to the top of the block, but not initialized. Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared. Using a let variable before it is declared will result in a ReferenceError .

Why does JavaScript allow hoisting?

Hoisting is JS's default behavior of defining all the declarations at the top of the scope before code execution. One of the benefits of hoisting is that it enables us to call functions before they appear in the code. JavaScript only hoists declarations, not initializations.


1 Answers

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.

like image 164
user113716 Avatar answered Nov 06 '22 19:11

user113716