Skip to content
GuideSphereCareers

JavaScript · All levels

JavaScript Interview Preparation: Scope, Closures, this and Async

Revise the JavaScript concepts that come up in almost every frontend interview — types, scope and hoisting, closures, this, prototypes, the event loop and promises.

Published 4 Dec 20263 min read

JavaScript interviews reward candidates who can predict what code will do and explain why. For each topic below, read the answer, then try writing a small example in your browser console.

Types and equality

JavaScript has primitive types (string, number, bigint, boolean, undefined, null, symbol) and objects (including arrays and functions).

Q: == vs ===? === compares without type conversion; == converts types first, which leads to surprises such as 0 == "" being true. Prefer ===.

Q: null vs undefined? undefined means a value was never assigned; null is an intentional "no value".

var, let and const

Scope Hoisted Re-assign Re-declare
var Function Yes (initialised as undefined) Yes Yes
let Block Yes, but in the temporal dead zone until declared Yes No
const Block Same as let No No

const prevents re-assigning the variable, not mutating the object it points to.

Closures

A closure is a function that remembers the variables from 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

Closures power data privacy, function factories, memoisation and event handlers.

Classic question: why does this print 3, 3, 3?

for (var i = 0; i < 3; i++) setTimeout(() => console.log(i));

var is function-scoped, so all callbacks share one i, which is 3 when they run. Using let creates a new binding per iteration and prints 0, 1, 2.

The this keyword

this depends on how a function is called:

  • Method call obj.fn() → this is obj.
  • Plain call fn() → undefined in strict mode (global object otherwise).
  • new Fn() → the new object.
  • fn.call(x) / fn.apply(x) / fn.bind(x) → x.
  • Arrow functions do not have their own this; they use this from the surrounding scope.

Prototypes and classes

Every object has an internal link to a prototype; property lookups walk up this prototype chain. class syntax is a cleaner way to write prototype-based inheritance.

The event loop

JavaScript runs on a single main thread. Long tasks block it.

  1. Synchronous code runs on the call stack.
  2. When the stack is empty, all microtasks run (promise callbacks, queueMicrotask).
  3. Then the next macrotask runs (e.g. setTimeout callbacks, I/O events).
console.log("A");
setTimeout(() => console.log("B"));
Promise.resolve().then(() => console.log("C"));
console.log("D");
// A, D, C, B

Promises and async/await

A promise represents a value that will be available later; it is pending, fulfilled or rejected. async functions always return a promise, and await pauses the function until a promise settles.

async function loadUser(id) {
  const res = await fetch(`/api/users/${id}`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return res.json();
}
  • Promise.all — waits for all, rejects on the first failure.
  • Promise.allSettled — waits for all, never rejects.
  • Promise.race / Promise.any — first to settle / first to fulfil.

Useful array methods

map (transform), filter (select), reduce (combine into one value), find, some, every, flat. Know which ones return a new array and which mutate (sort, splice, push mutate).

Debounce and throttle

  • Debounce: run only after events stop for N ms (search-as-you-type).
  • Throttle: run at most once every N ms (scroll handlers).
function debounce(fn, ms) {
  let t;
  return (...args) => {
    clearTimeout(t);
    t = setTimeout(() => fn(...args), ms);
  };
}

Practice checklist

  • Implement debounce, throttle and a simple memoize.
  • Write a polyfill for Array.prototype.map.
  • Predict the output of event-loop ordering puzzles.
  • Explain shallow vs deep copy ({...obj} vs structuredClone(obj)).
  • #javascript
  • #frontend
  • #web