The worst ‘javascript interview prep’ article ever

Lauren Wszolek
4 min readNov 30, 2016

--

I’m linking this here, but you should not believe any of these lies.

Question 1…. I mean, ok. It’s not a great answer, but it’s not deceiving you the way question 2 is.

I’ll cut and paste question 2 here for you so you don’t need to dirty your browser history:

Question 2

What will be the output of the code below?

var y = 1;
if (function f(){}) {
y += typeof f;
}
console.log(y);

The output would be undefined. The if condition statement evaluates using eval, so eval(function f(){}) returns function f(){} (which is true). Therefore, inside the ifstatement, executing typeof f returns undefined because the if statement code executes at run time, and the statement inside the if condition is evaluated during run time.

var k = 1;
if (1) {
eval(function foo(){});
k += typeof foo;
}
console.log(k);

The code above will also output undefined.

var k = 1;
if (1) {
function foo(){};
k += typeof foo;
}
console.log(k); // output function

Oh my god, no. I felt horror that anyone could use this as a resource to better understand javascript — this isn’t even the first time someone lied to me in a javascript tutorial today!

Part of this is true: if you run that first code snippet, you will indeed get undefined. But not for the reasons listed, not even close. At first, I was confused — sure it doesn’t work, but why not? If conditional expressions don’t allow variable declarations, but they allow assignments, function calls, etc., why not this? And why does it pass into the loop at all?

Here’s my response (with better formatting & sources cited at the end) . It took about two hours until I was totally satisfied, dipping into the es6 spec and my first trip into the v8 codebase (no answers, but it was fun to poke around):

Hi,

I am only on question 2, but there are serious errors.

First, let’s say eval was being used inside of an if statement (it is not): the reasoning is faulty. eval() only interprets strings — if anything else is passed in, it simply returns it. It is not evaluated. So because it was not evaluated, foo remains undefined. It has nothing to do with runtime. If eval was happening here, then it would make sense we entered the loop — we got back a non falsey value, ie function foo(){}, yet it was not evaluated, so trying to call foo would result in an error.

so eval(function foo(){}) returns function foo(){} without evaluating it.

The given explanation also seems to imply that ‘eval’-ing something means it would not enter the current scope, this is not true. eval-uated strings immediately enter the scope (use with caution). If we had done instead:

eval(“function foo(){}”) we would have access to foo by name within the entire scope the eval occurred.

However, eval is not used. Let’s revisit the code snippet and see what’s really happening:

if ( function foo(){} ) { whatever }

if statements accept an expression as argument/condition. I made that bold so it’s apparent this is a special term and not the colloquial concept of an expression. function foo(){} can either be a function declaration, or the same snippet of code can be a function expression, depending on where the text appears in the line of code. Function declarations occur when function is the first text on the line and nothing precedes it, meaning it is defined in the current scope. Here is a function declaration:

function foo() {}

However, function foo() {} is a function expression in this context. if takes only expressions. Declarations are not expressions. You can’t say if(var foo = 12), because var foo = 12 is not an expression. So when this code is run, it is creating function foo(){} as an expression, which is valid syntax in an if statement. Names are not bound to the current scope with function expressions, they are only bound within the function itself. function foo(){} is an empty function — so this name is never used, and the function becomes inaccessible immediately after this section of code is run.

sources:

eval returns any non-string argument without processing it:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf#page=98

if statements accept expressions:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

named function expressions have their name scoped to within themselves :
https://github.com/getify/You-Dont-Know-JS/blob/31e1d4ff600d88cc2ce243903ab8a3a9d15cce15/scope%20%26%20closures/ch3.md#functions-as-scopes

I can fix the rest of the issues if you send me whatever paycheck they gave you for writing this.

Maybe I’ll do the rest, because wow does it look like there are a lot of errors in the rest of the questions. So many, it almost seems like an anti-javascript plant trying to misinform and frustrate people…. Though, like my last comment says, I kinda want to be paid for doing your work properly.

Actually, Mr UI Engineering Lead, maybe I should just get your job: I am in the market.

--

--

Lauren Wszolek

Hello [object Object], my old friend, in console debugging again.