The 5 types of JS Developers I find in Interviews

One of my favorite questions to ask JS developers, regardless of their level of experience is, Given:

for (var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}

what would this print?

I usually expect this to be an ice-breaker, and start talking about closures and dig into how well someone understands JS. I can follow this up with

  • What happens if the 1000 is changed to zero?
  • What if I remove the second argument to setTimeout completely
  • How do I get this to work so that it prints 0 to 9 after 1 second?

The last question is the one that can make me laugh, cry and doubt myself! The answers have varied so drastically, that I have started labeling the candidates based on this. Here are the four common types of candidates I end up seeing:

The “Are you Serious” Candidate?

This JS developer is a rockstar. With a sneering / condescending look, he explains the event loop, what a closure is, and gives you the answer within the next one minute. And you are left with the rest of the interview trying to recover your credibility! And the feeling that

The “Creative” Candidate

This candidate struggles with the question for a bit, has head of closures and the like, but hasn’t really used or worked with them. In a fit of desperation, inspiration strikes, and the candidate proposes the following:

setTimeout(function() {
for (var i = 0; i < 10; i++) {
console.log(i);
}
}, 1000);

which to be fair, does accomplish the original task set out. It’s more like,

At this point, I give them points for creativity, and cry within my head.

The “Funception” Candidate

This candidate gets closures! But he also gets functional programming. And when the two combine, you have Funception. The code ends up looking like the following:

for (var i = 0; i < 10; i++) {
setTimeout((function(i) {
return function() {
console.log(i);
};
})(i), 1000);
}

This candidate, instead of creating a simpler closure, ends up passing a self-invoking function to setTimeout that takes i as an argument, and then returns a function that actually does the console.log. If I’m lucky, they stop at that point, if not, they do find more ways to include functions.

At this point, I feel like I’m the one giving the interview, trying to make sense of their code!

The “Screw this shit” Candidate

The “Creative” candidates atleast complete the task at hand, which is to display 0 to 9 after a second. This last set of candidates are the one so in over their heads, that after struggling for a good 5 minutes, the solution they come up with is mind-numbingly simple:

for (var i = 0; i < 10; i++) {
console.log(i);
}

Where did the setTimeout go?

Oh, it was causing too much issues, so I simplified the code.

What if I want the logs to be printed after one second?

Why would you ever want that in real life?

At this point, I’m either in a philosophical discussion on the meaning of life, or the candidate is like

I agree! I wholeheartedly agree!

The “Nuh Uh” Candidate

The last set of candidates are the ones very new to JavaScript, or the experienced JS developers who have only directly learnt and worked with frameworks, without spending time learning the underlying JS bits. These are the ones who will respond to the original question of what the code will print with

0 to 9, obviously, but after a second

Nothing you say will ever convince them of anything otherwise. If anything, they will judge you and decide that you are really not worthy of interviewing them. Kind of like the first type of candidates, but in this case, I feel more like

Any others?

Have you seen any other type of candidate? Do let me know, and I would love to add more to this list!