Uberflip interview question

What will the code below output to the console and why? (function(){ var a = b = 3; })(); console.log("a defined? " + (typeof a !== 'undefined')); console.log("b defined? " + (typeof b !== 'undefined'));

Interview Answer

Anonymous

27 Dec 2016

The output with show that a is not defined and that be is defined. Here the reason why ... The declaration line (var a = b = 3;) is inside of a function. Because of this, and due to the syntax used, only the `a` is seen as a local variable. The variable `b` however is declared as a global variable. So onlye `b` will be seen outside the function. The proper way to keep `a` and `b`local to the function would have been `var a = 3, b = 3;`

1