Skip to content
Laviyx Learn

Try

JavaScript course menu

Advanced JavaScript Interview Questions and Answers

Advanced JavaScript interview questions with answers: the prototype chain and prototypal inheritance, how new works, the event loop in depth, memory leaks and garbage collection, WeakMap, generators, module systems, immutability, currying and memoization, and security.

Advanced5 min readUpdated
On this page

Advanced questions check whether you understand the language's internals and can reason about performance, memory and architecture. They are common for senior front-end, full-stack and Node.js roles.

1. Explain the prototype chain.#

Show solution

Every object has an internal link to a prototype object. When a property is not found on the object, the lookup continues up the chain (obj → Constructor.prototype → Object.prototype → null). Methods live once on prototypes and are shared by all instances. Classes are syntax over this: methods go on Class.prototype, and extends links one prototype to another.

2. What does the new keyword do?#

Show solution

new Fn(args):

  1. creates a new empty object;
  2. sets its prototype to Fn.prototype;
  3. calls Fn with this bound to the new object;
  4. returns the new object, unless the function explicitly returns another object.
JavaScript
function myNew(Fn, ...args) {
  const obj = Object.create(Fn.prototype);
  const result = Fn.apply(obj, args);
  return result !== null && typeof result === "object" ? result : obj;
}

function User(name) { this.name = name; }
User.prototype.hi = function () { return `Hi ${this.name}`; };

console.log(myNew(User, "Asha").hi());
Output
Hi Asha

3. Describe the event loop in detail.#

Show solution

JavaScript runs on a single call stack. Asynchronous work is handled by the host (browser or Node.js), which queues callbacks. The event loop repeatedly: runs one task (macrotask) from the task queue, such as a timer, an I/O callback or an event; then runs all queued microtasks (promise reactions, queueMicrotask, MutationObserver), including ones added during that step; then, in browsers, may render (with requestAnimationFrame callbacks before painting). A long synchronous task or an endless chain of microtasks blocks rendering and input.

4. What causes memory leaks in JavaScript?#

Show solution

The garbage collector frees objects that are no longer reachable. Leaks happen when references are kept unintentionally: forgotten timers and intervals, event listeners on removed elements or on window that are never removed, growing caches or global arrays, closures holding large objects, and detached DOM nodes still referenced from JavaScript. Fixes include removing listeners (or using AbortController signals), clearing intervals, bounding caches, and using WeakMap/WeakRef where appropriate. The browser developer tools' Memory panel finds them with heap snapshots.

5. What are WeakMap and WeakSet for?#

Show solution

They hold object keys weakly: if nothing else references the key object, it can be garbage-collected and its entry disappears. They are not iterable and have no size. Typical uses are attaching metadata or caches to objects you do not own (such as DOM nodes) without causing leaks.

6. What is the difference between ES modules and CommonJS?#

Show solution

ES modules (import/export) are the standard: static structure analysed before execution (enabling tree shaking), asynchronous loading, live bindings, strict mode by default and top-level await. CommonJS (require/module.exports) is Node's original system: synchronous, dynamic, with exports copied as values. Node supports both; ESM is the modern default for new code.

7. What is currying? Implement a generic curry function.#

Show solution

Currying transforms a function of several arguments into a chain of functions that each take some of them:

JavaScript
function curry(fn) {
  return function curried(...args) {
    return args.length >= fn.length
      ? fn.apply(this, args)
      : (...more) => curried.apply(this, [...args, ...more]);
  };
}

const volume = curry((l, w, h) => l * w * h);
console.log(volume(2)(3)(4), volume(2, 3)(4), volume(2)(3, 4));
Output
24 24 24

8. How would you make an object immutable?#

Show solution

Object.freeze(obj) prevents adding, removing or changing its properties, but only at the top level; nested objects stay mutable, so deep freezing needs recursion. In practice, immutability is usually achieved by never mutating: creating updated copies with spread, map, filter, toSorted and with, which is what frameworks like React rely on to detect changes.

9. What are generators used for?#

Show solution

Generator functions (function*) can pause at yield and resume later. They make custom iterators easy, produce lazy or infinite sequences that compute values only on demand, and (with async function* and for await...of) consume paginated APIs or streams. Historically they were also used to build async/await-like control flow.

10. How do you prevent XSS in a JavaScript front end?#

Show solution

Never insert untrusted data as HTML: use textContent, setAttribute and DOM APIs rather than innerHTML, insertAdjacentHTML or document.write. Rely on frameworks' automatic escaping and avoid their raw-HTML escape hatches. If HTML must be rendered, sanitise it with a well-maintained library. Add a Content Security Policy, avoid eval and new Function, keep secrets out of client code, and validate on the server.

11. Explain microtask starvation.#

Show solution

Because the event loop empties the entire microtask queue before moving on, a microtask that keeps scheduling more microtasks (for example, a recursive promise chain without awaiting a timer or I/O) prevents timers, events and rendering from ever running, freezing the page. Breaking work into tasks with setTimeout, or yielding with await on something that involves a task, lets the browser stay responsive.

12. What is the difference between Object.freeze, Object.seal and const?#

Show solution

const only prevents reassigning the variable. Object.seal prevents adding or removing properties but allows changing existing ones. Object.freeze prevents adding, removing and changing properties. Both seal and freeze are shallow.

Keep going#

Practise the coding challenges, or browse every question with hints on the JavaScript interview page.

Finished this lesson?