Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Back to Interview Preparation
Common JavaScript Interview Questions

Common JavaScript Interview Questions

Interview PreparationBy Muhammad Fareed
interview-preparationcommonjavascriptinterviewquestions

Short answer: JavaScript interviews usually test core concepts: scope and closures, this, the event loop, promises and async/await, prototypes, and array methods. Below are the most frequent questions with concise answers and examples.

1. What is the difference between var, let and const?

varletconst
ScopeFunctionBlockBlock
ReassignYesYesNo (the binding; objects can still be mutated)
HoistingHoisted, initialized as undefinedHoisted, but in the "temporal dead zone" until declaredSame as let

Prefer const by default, let when you need to reassign, and avoid var.

2. What is a closure?

A closure is a function that remembers the variables of the scope where it was created, even after that scope has finished.

function counter() {
  let count = 0;
  return () => ++count;
}
const next = counter();
next(); // 1
next(); // 2

Uses: private state, function factories, callbacks. See closures explained.

3. What is hoisting?

Declarations are processed before code runs. Function declarations are fully hoisted; var variables are hoisted as undefined; let/const exist but cannot be accessed before their declaration.

4. How does this work?

  • In a method call, this is the object before the dot.
  • In a plain function, it is undefined (strict mode) or the global object.
  • Arrow functions do not have their own this; they use the surrounding one.
  • You can set it with call, apply and bind.

5. == vs ===

=== compares value and type without conversion. == converts types first (0 == '0' is true). Use === in almost all cases.

6. Explain the event loop

JavaScript runs on a single thread with a call stack. Asynchronous work (timers, network) is handled by the environment, and callbacks are queued. The event loop moves them onto the stack when it is empty. Promise callbacks (microtasks) run before timers (macrotasks).

console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
// Output: 1, 4, 3, 2

7. What are promises and async/await?

A promise represents a future value in one of three states: pending, fulfilled or rejected. async/await is syntax that lets you write promise code like synchronous steps.

async function load() {
  try {
    const res = await fetch('/api/data');
    return await res.json();
  } catch (e) {
    console.error(e);
  }
}

Read more in promises and async/await.

8. What is prototypal inheritance?

Objects have a hidden link to another object, their prototype. When a property is not found, JavaScript looks up the prototype chain. ES6 class syntax is a cleaner way to use prototypes.

9. map vs forEach vs filter vs reduce

MethodReturnsUse
forEachNothingSide effects
mapNew array, same lengthTransform
filterNew array, subsetSelect
reduceOne accumulated valueSum, group, build objects

See the array methods cheat sheet.

10. Shallow copy vs deep copy

const shallow = { ...obj };            // nested objects are still shared
const deep = structuredClone(obj);     // copies nested data (no functions)

Common coding exercises

// Debounce
function debounce(fn, delay) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delay);
  };
}

// Reverse a string
const reverse = (s) => [...s].reverse().join('');

// Remove duplicates
const unique = (arr) => [...new Set(arr)];

// Flatten an array
const flat = [1, [2, [3]]].flat(Infinity);

More topics to review

  • Modules (ESM vs CommonJS), destructuring, spread and rest; see ES6 features.
  • DOM events, bubbling and delegation.
  • Currying, memoization and higher-order functions.
  • null vs undefined and truthy/falsy values.

Tips for answering

  1. Start with a one-sentence definition, then give a short example.
  2. Mention pitfalls and real use cases.
  3. If unsure, reason out loud rather than guess silently.

Read the general plan in how to prepare for a technical interview.