Javascript Interview Questions

Nobody knows Javascript 100%. Nobody. I've met haughty people who pretend they do, but they don't. I also sometimes find interviews a little invalidating, like maaan you caught me out on a total edge case. Or sometimes, when I've been conkers deep in Elasticsearch or CSS or some other seemingly random task I end up with, and a simple question comes my way and I'm hit with a complete blank. And it's a question I knew! Stupid brain as homer would say. Anyways, this is a list of refreshers more than anything for interviews I may do.

I'm going to start with the basics, then work up to more advanced stuff.

use strict

What does the 'use strict' directive do? It tells the compiler/interpreter to utilise the strict variant of the ECMAScript when parsing. This spots global variables, prevents 'this' coercion, disallows duplicate property names.

Null

There is a bug in the language at the moment with null. Null should be null, but it isn't, it is an object.

var bar = null;
typeof bar === 'object' //true

Declared variables, undeclared, and undefined

x === undefined //throws a reference error 'x is not defined'
let x;
x === undefined //true

Truthy and Falsy stuff

A function is an object, objects are evaluated to true:

!!function f(){} === true

Usually you get some questions about the == and === operators. === is a lot faster because the == operator runs code to cast the type and then check values. Like "1" == 1 returns true.

1 === 1; //O(1)
"1" == 1'; //O(n)

Return statement

Crockford is great. His example of the return statement issues in javascript is classic. Javascript as automatic semicolon insertion, so the below would return undefined.

return 
{
  name: 'Billy Bob'
}

Number quirks

Because javascript uses the IEE754 standard, and numbers with computers are not great - in fact there is mathematical theory that numbers are constantly changing. But anyway, you have to be careful when it comes to numbers. A common question is something like this, will the below be true or false:

(0.033 + 0.0312) === 0.0642

It would be false, because the result would be:

0.06420000000000001

References