DEV Community

Cover image for 7 JavaScript Interview Questions to Test How Smart You Really Are
Arnold Gunter
Arnold Gunter

Posted on

7 JavaScript Interview Questions to Test How Smart You Really Are

JavaScript is everywhere. It runs in your browser, your phone apps, maybe even your fridge.

But do you really understand it? Or are you just copying code from ChatGPT and hoping it works?

Well… Here are 7 questions interviewers love to ask to find out how smart (or sneaky) you really are.

Some look easy. Some are traps (the easy ones too). Let’s go!


👉 Join 200+ web developers already learning smarter HTML, CSS & JavaScript — weekly and free forever.

Get bite-sized tips, rare tricks, and tools you won’t find in tutorials. Subscribe to the Weekly Web Dev Digest now.


1. Can you count to 10… the JavaScript way?

Interviewers love this one:

“Write a function that counts from 1 to 10, logging one number per second.”

Easy? Try it.

Many candidates write a for loop with setTimeout() inside. Like this:

for (let i = 1; i <= 10; i++) {
  setTimeout(() => {
    console.log(i);
  }, 1000);
}
Enter fullscreen mode Exit fullscreen mode

But guess what? That doesn’t work.

A working solution:

for (let i = 1; i <= 10; i++) {
  setTimeout(() => {
    console.log(i);
  }, 1000 * i);
}
Enter fullscreen mode Exit fullscreen mode

Here’s the deal:

setTimeout() doesn’t repeat an action. It waits once, then runs your function.
The second parameter is the delay in milliseconds — how long to wait before running the function.

So if we just wrote:

setTimeout(() => console.log(i), 1000);

It would log all numbers after 1 second, all at once. Why? Because every timeout runs after the same delay.

But with:

setTimeout(() => console.log(i), 1000 * i);

Each i gets its own unique delay:

  • When i = 1, delay = 1000 * 1 = 1 second
  • When i = 2, delay = 1000 * 2 = 2 second
  • When i = 10, delay = 1000 * 10 = 10 seconds

So you’re telling JavaScript:

“Hey, log 1 after 1 second, 2 after 2 seconds, ..., 10 after 10 seconds."

Boom — counting!

2. Can you handle async like a pro, or just panic?

You’re asked to:

“Fetch data from 3 APIs, then do something after they all finish.”

This tests if you understand asynchronous code.

In JavaScript, things like fetch() or setTimeout() don’t happen instantly. They start and finish later. If you try to use the result right away, it won’t be there yet.

Bad approach: nested callbacks.

Good approach:

Promise.all([fetchA(), fetchB(), fetchC()])
  .then(([resA, resB, resC]) => {
    // All done, now do stuff
  });
Enter fullscreen mode Exit fullscreen mode

Or better:

const data = await Promise.all([fetchA(), fetchB(), fetchC()]);
Enter fullscreen mode Exit fullscreen mode

Promise.all() waits for all promises to finish. Only when everything is done, it gives you the results. No callback hell, no confusion. Just clean and smart.

3. What is ‘this’? (No, really.)

this in JavaScript is tricky. It changes depending on how you call a function.

If you write:

const person = {
  name: 'Sam',
  greet() {
    console.log('Hi, I am ' + this.name);
  }
};
person.greet(); // “Hi, I am Sam”
Enter fullscreen mode Exit fullscreen mode

this refers to person.

But if you do:

const greet = person.greet;
greet(); // Uh-oh... “Hi, I am undefined”
Enter fullscreen mode Exit fullscreen mode

Now this doesn’t point to person anymore. It points to the global object (or undefined in strict mode).

Arrow functions are different — they don’t have their own this. They use the this from the place where they were created.

4. What are closures, and can you use them without Googling?

Closures are what happen when a function “remembers” variables from the place where it was created.

Example:

function outer() {
  let count = 0;
  return function inner() {
    count++;
    console.log(count);
  };
}
const counter = outer();
counter(); // 1
counter(); // 2
Enter fullscreen mode Exit fullscreen mode

Even though outer() has finished running, inner() still remembers the count variable. That’s a closure. It lets functions keep state. Super useful for things like counters, timers, or private variables.

5. Do you even ‘bind’, bro?

Here’s the deal:

doThis('foo', something.doThat);
Enter fullscreen mode Exit fullscreen mode

This passes a reference to doThat. But:

doThis('foo', () => something.doThat());
Enter fullscreen mode Exit fullscreen mode

This calls doThat() right away and passes the result.

If doThat depends on this, passing it directly might break it. That’s where .bind() helps:

doThis('foo', something.doThat.bind(something));
Enter fullscreen mode Exit fullscreen mode

Now the function will always use the correct this.

Knowing when to pass a function vs. call it, and how this behaves, is key to writing clean, working code. If you pass a function and it loses its context, weird bugs show up.

6. Is a function a first-class citizen?

In JavaScript:

  • You can put a function in a variable.
  • You can pass it to another function.
  • You can return it from a function.

That’s what first-class means.

Example:

function greet(name) {
  return 'Hello ' + name;
}

function welcome(fn) {
  console.log(fn('Alice'));
}
welcome(greet); // Hello Alice
Enter fullscreen mode Exit fullscreen mode

Why it works:
JavaScript treats functions like any other value. This lets you build higher-order functions, callbacks, and more powerful patterns.

7. Can you explain prototypal inheritance without sweating?

JavaScript doesn’t use class-based inheritance like Java or C++.

It uses prototypes.

Every object can point to another object — its prototype. When you try to access a property that doesn’t exist, JavaScript looks up the prototype chain.

Example:

const animal = {
  speak() {
    console.log('Animal sound');
  }
};

const dog = Object.create(animal);
dog.speak(); // Animal sound
Enter fullscreen mode Exit fullscreen mode

Why it works:
dog doesn’t have a speak() method. But its prototype (animal) does. JavaScript finds it up the chain and uses it. This is how inheritance works under the hood.

Final Thoughts
JavaScript is a fun language, but it’s full of little traps. If you can answer these questions without copying from ChatGPT (wait a minute…), you’re well on your way to JavaScript mastery.

Just remember: knowing what works is good. Knowing why it works is even better.

Good luck!

Top comments (0)