4

I found a snippet of code that I am having trouble understanding. If someone can take me through it, it would be nice. Heres the code, I will add my attempt below

main()

function main()
{
    var name = "bob";
    doSomething(function()
    {
        if (false)
        {
            var name = "fred";
        }

        // Were you expecting it to be "bob"?!
        alert(name);
    });
}

function doSomething(callbackFunction)
{
    callbackFunction();   
}

OUTPUT

undefined

My attempt

First within main(), we set a name to bob. Then the function doSomething calls the anonymous function we pass in.

Now heres where I get a bit confused.

What does if (false) check? If what is false, do we reset our name to fred?

We then proceed to alert the name, which well... is apparently undefined.

bill
  • 378
  • 2
  • 9
  • did you run this snippet in a browser? You'll only get the `alert` in the browser. – Brandon Aug 29 '16 at 17:37
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting – apsillers Aug 29 '16 at 17:38
  • 3
    It's not checking anything for false. It's showing you variable hoisting. – Dave Newton Aug 29 '16 at 17:38
  • I got `undefined` in a browser. and @DaveNewton, what do you mean? Whats the point of the if statement then – bill Aug 29 '16 at 17:40
  • I'll read the other link, mabye then Ill understand – bill Aug 29 '16 at 17:41
  • if false, then it creates a `local variable` name with value `fred` it doesnt update. – Arjun Aug 29 '16 at 17:42
  • if WHAT is false? thats what I dont get – bill Aug 29 '16 at 17:43
  • @bill The `if (false) { ... }` block just means "here is some code in the function that will never ever run". It is testing if the boolean primitive `false` is a truthy value, which is never the case. You could also write this as `var foo = false; if (foo) { ... }` but that's a bit longer. – apsillers Aug 29 '16 at 17:44
  • On top of the code there was a description saying `If you define a variable in the outer scope, and then have an if statement define a variable inside the function with the same name, even if that if branch isn't reached it is redefined.`, so is that wrong? – bill Aug 29 '16 at 17:46
  • 1
    The duplicate here is a little more complex than your example, so I'll give a quick, more targeted explanation: `var name = "fred"` does two things: (1) it *declares* a new variable in the local function scope, and (2) it sets that variable to the value `"fred"`. Think of it as two separate statements, `var name;` and `name = "fred";`. However, due to hoisting (see my link above), all `var` declarations are hoisted to the top of their scope. The `var name` part is lifted out of the `if(false)` block and *does* run. However, its value is never set (because `name="fred"` does not run). – apsillers Aug 29 '16 at 17:48
  • How does `if(false)` run? Is this related to hoisting as well? – bill Aug 29 '16 at 17:56
  • No, I mentioned above: `if(false)` is used here the same as `var foo = false; if (foo) { ... }`. (If you don't understand how that code operates, then you may need to review how JavaScript syntax works for boolean literals, variable assignment, and/or `if` statements; I cannot make it any simpler) – apsillers Aug 29 '16 at 17:59
  • Ok, ill try looking at boolean stuff again. Your `var foo = false` example makes sense to me, its just that I didnt really get if WHAT is `false` do ` {}` – bill Aug 29 '16 at 18:03
  • @Arjun No, it creates a local variable `fred` no matter what, but it's not initialized, because the initialization happens in the `if` statement body that will never execute. – Dave Newton Aug 29 '16 at 18:04
  • @bill The `if` statement is `if (false)`, and that's all. `false` will never evaluate to `true` or anything truthy. The body of the `if` statement will never execute. Variable hoisting in JS means that the variable `fred` *will* exist, be set to undefined, at the start of the function's body: that's just how JS works. You're getting hung up on a simple `if` statement where the expression under test is `false`. The "point" of the `if` statement is to show you variable hoisting. – Dave Newton Aug 29 '16 at 18:05
  • @bill An `if(...)` statement says "If `...` is true, then...", so therefore `if(false)` says, "If the *boolean primitive `false` as defined by the JavaScript language* is true, then..." That's it; that's the whole thing. The point is that the code inside the block will never run (since `false` will never be true) – apsillers Aug 29 '16 at 18:09
  • So moral of story is if I reassign a variable already existing in the global scope, in an if statement. Even if the if statement is never actually true, javascript will reassign the global scope to be undefined? – bill Aug 29 '16 at 18:09
  • @bill You're getting closer, but: (1) nothing is being "re"-assigned; the `var name` creates a new variable, local to the containing function (here, the anonymous function being passed into `doSomething`) and the value of a value is by default `undefined` until set to something else. The creation of this variable "shadows" the visibility of the variable called `name` in the outer scope, so it is not accessible. (2) This effect then is not global, but only local to the function. – apsillers Aug 29 '16 at 18:12
  • OoOoOoOooOoOoOooOo, so basically `if(false) `is NOT true,but due to hoisting V8 interprets this as simply `var name;`, but since `name` isn't set, javascript sets it to undefined hence `undefined`? This is within the `doSomething()` scope. – bill Aug 29 '16 at 18:20
  • @DaveNewton oh yes! Thanks for the clarification! – Arjun Aug 29 '16 at 20:01

0 Answers0