Interview Q&A / JavaScript

INTERVIEW Q&A · 71 QUESTIONS

JavaScript interview questions

Closures, the event loop, this, promises and the core language questions every frontend round asks.

  1. How does the this keyword work in JavaScript?

    this is decided by how a function is called, not where it is written. A method call obj.fn() sets this to obj; new sets it to the new object; call, apply and bind set it explicitly; a plain call gives undefined in strict mode. Arrow functions have no own this and use the surrounding one.

    IntermediateIn-depth3 min read
  2. What is the difference between Object.freeze, Object.seal and Object.preventExtensions?

    preventExtensions blocks adding properties. seal also blocks deleting and reconfiguring, but allows changing values. freeze blocks everything, so values are read-only too. All three are shallow.

    IntermediateQuick answer1 min read
  3. Write a function that deep clones an object, including nested arrays and circular references.

    Return primitives as they are. For objects and arrays, create an empty copy, record it in a WeakMap so circular references reuse it, then recursively clone each property. In modern runtimes, structuredClone does this for you.

    IntermediateQuick answerCoding1 min read