Register your Account

JavaScript Interview Question

We recently came across a good JavaScript interview question. We won’t disclose the name of the company using it, but we hope that this helps interviewees test their knowledge, and employers looking for good ways to design questions:

Look at the following code. What does it do? What happens if you click on the li elements?

Establish what happens when you click on the li elements. Can you guess? Ask the interviewee to explain why this happens.

Ask how the code can be fixed so that clicking on each element outputs an alert corresponding to its order in the list (the desired behavior)

This simple exercise covers a lot of bases in evaluating someone’s JavaScript understanding in a short period of time. What do people think? What are good followup questions?

Discussion

Is it a good question? It is a rather simple question, so it depends on who you are trying to hire. A lead JS engineer? No. A junior webdev? Sure.

A similar, and good follow-up question (one that I ask all the time) is… instead of a list, I present them with:

<a>
<b>
<c>
<d>

Next I ask them to add event listeners to alert() the content of each <div> when clicked. Often they do code that looks the same as the code in the example. Then, I explain why adding an event listener to every single element can be expensive, so what’s a better way to do it?

Answer: Add a wrapper around the 4 divs, and add one event listener to that. This follow-up could be adapted to your question by saying they only get 1 event listener to bind, and look for them to attach it to the <ul> instead.