21 (sort of) Essential JavaScript Interview Questions

6I was recently challenged to spend an hour going through the javascript questions/answers on this link Javascript Questions to determine whether or not these so-called “21 Essential JavaScript Interview Questions” really are essential.

I’m mixed about these questions.  Many of these are good evaluators for some core JavaScript knowledge, but others seem like they’re just “gotchas” to see if you know the esoteric quirks of javascript which are just not applicable in day-to-day javascript use.

For the most part, these are good questions.  Even using all the questions, including the gotchas, would give a good indicator of the working javascript knowledge of the person.

Now let me pass the challenge along to YOU before reading further. Attempt to answer all 21 questions in an hour. I didn’t have any trouble getting through them in that time span.

Once you’ve completed them and gathered your own thoughts, check out what I have to think about on each question below (and for the record, the team as a whole came to this same general consensus):

Question 1: What is the difference between undefined and not defined in JavaScript?

This is a good one, very core to know in javascript

Question 2: What will be the output of the code below?

This feels like a “gotcha” question, because the example provided will never ever be done in a real-world situation.   Reading through the explanation is good, I guess (I didn’t know that ‘if’ statements were checked using the eval function) but again, that’s just one of those weird javascript quirks that someone may or may not know, but doesn’t really reflect on whether or not they have a good working knowledge of javascript.

Question 3: What is the drawback of creating true private methods in JavaScript?

This is also a good, core javascript design question.  It actually made me look up if there was a better way to implement true private methods without being a resource hog, so good for getting me curious.

Question 4: What is a “closure” in JavaScript? Provide an example

I like what this question is asking, I just don’t like the way it’s asking it.  I definitely knew the concept of closure (a function withint a fuction that has access to the parent function scope) but didn’t know it knew the term “closure”.  Maybe others do?  I feel like asking about functions inside functions and scope would be better than just saying “what is closure?”

Question 5: Write a mul function which will produce the following outputs when invoked: javascript console.log(mul(2)(3)(4)); // output : 24 console.log(mul(4)(3)(4)); // output : 48

This is good.  The answer is very simple and shows a good understanding of a function being able to return a function

Question 6: How to empty an array in JavaScript ?

This is an excellent question.  It’s absolutely core to know how to do this in javascript, and there are multiple ways to do it (bonus if the person knows more than one method)

Question 7: How do you check if an object is an array or not?

Another good question, and not just checking if an object is array – checking the type of ANY object (checking if an object is a function is a really good one that is done pretty regularly, for example)

Question 8: What will be the output of the following code?

This is good but might be better asked as “what does the ‘delete’ keyword do in javascript?”.  It’s good to know delete affects properties on an object and nothing else.

Question 9: What will be the output of the following code?

This is pretty much the same as question 8?  If you know what the delete keyword does from question 8, you know what it’ll do here…seems redundant.  I guess this is just testing if the user knows how delete will affect different types of variables?

Question 10: What will be the output of the code below?

This is the #1 use-case of the delete keyword in javascript, so very good core knowledge.

Question 11: What will be the output of the code below?

Ok this one tripped me up, I didn’t know that the delete keyword wouldn’t remove a prototype property on an object.  So it’s a good question I guess?  I wouldn’t put a ton of stock in it, though – of all the delete keyword questions (8,9,10 and 11), I think 10 was the best question, but again, I think a better question would be “what does ‘delete’ do?” or “how would you remove a property from an object?”

Question 12: What is undefined x 1 in JavaScript?

I think this is a bad question.  It’s browser-specific, and very unintiutite – in other words, another quirky javascript-ism that isn’t the best reflection of a user’s working knowledge.

Question 13: What will be the output of the code below?

This is good question but I don’t like the way this test presented it.  It’s really just asking “if you ‘delete’ a value from an array, does the array shrink?”

Question 14: What will be the output of the code below?

This is EXCELLENT (in fact one of the first questions I expected to see on this list).  One of the big challenges of javascript is how loosely typed it is, so knowing how different object types interact is good to know.  THAT BEING SAID, I still have to look up to remind myself what types take precendence when different types are interacting.  If I asked this question to a candidate, I probably wouldn’t be looking to see if they know all the type interaction rules off the top of their head, but instead know that there ARE weird type interactions and are aware of them.

Question 15: What will be the output of the code below?

Another good question to know when doing a multi-assignment in javascript

Question 16: What will be the output of the code below?

I feel this is a middle-of-the-road question.  I see this basically testing if the user knows that bar() wouldn’t be defined outside of being assigned to foo, which displays a good understanding of the way function assignment to variables work, but isn’t really real-world (for example, I’ve never assigned a function with a name to a variable – I don’t really see a reason to do that, since you can’t use the function’s name to invoke the function outside of the function itself; you have to use the variable)

Question 17: What is the difference between the function declarations below?

Another really good question.  This displays knowledge of run-time vs parse-time evaluation.

Question 18: What is function hoisting in JavaScript?

I have to admit I didn’t know this one and had to look it up.  Now that I do know what hoisting is, it feels somewhat like a javascript quirk.  In my opinion, you should never be defining a variable after you’re using it in the code but at the same time, knowing what javascript does in that situation might be useful?  I’m on the edge about this question.

Question 19: What will be the output of code below?

This is a text-book “gotcha” question.  To know the answer to this, you need to know what hoisting is, and if you know what hoisting is, the answer to this should be fairly obvious if you think about it.  The question itself even says it’s design to trip up javascript newbies.  It might be useful to ask someone after explaining what hoisting is (or if they claim they know), but I don’t think it’s the best indicator of working javascript knowledge

Question 20: What is the instanceof operator in JavaScript? What would be the output of the code below?

Instanceof is a good keyword to know in javascript, although I feel like the way the question is using it is another gotcha.  I’d just ask if the user knows what instanceof does and provide some simpler tests than the ones shown here.

Question 21: If we have a JavaScript associative array, How can we calculate the length of the above associative array's counterArray?

I liked this question.  It’s not really a “core knowledge” question in my opinion, and I don’t really see a common real-world application, but if the user has a decent amount of javascript experience there are many ways to get to the answer.  I sort of see it as a bonus question that if you have the fundamentals down, you should be able to figure out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s