Interview Q&A / JavaScript
JavaScript interview questions
Closures, the event loop, this, promises and the core language questions every frontend round asks.
Prefer rapid revision? Practise JavaScript short interview answers →
- 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 - What are prototypes in JavaScript, and how does prototypal inheritance work?
Every object has a hidden link to another object called its prototype. When you read a property that the object does not have, JavaScript looks it up along this prototype chain. Classes are syntax built on top of this: methods live on the prototype and are shared by all instances.
AdvancedIn-depth3 min read - How do you check whether a property exists on an object?
Use Object.hasOwn(obj, key) for own properties and the in operator to include inherited ones. Comparing to undefined is unreliable, because a property can exist and hold undefined.
BeginnerQuick answer1 min read - Is JavaScript pass by value or pass by reference?
JavaScript is always pass by value. For objects, the value passed is a reference, so a function can change the object's contents but cannot make the caller's variable point to a different object.
BeginnerQuick answer1 min read - What do Object.keys, Object.values, Object.entries and Object.fromEntries do?
keys gives an array of own enumerable property names, values gives their values, and entries gives [key, value] pairs. fromEntries does the reverse and builds an object from pairs, which makes transforming objects easy.
BeginnerQuick answer1 min read - What is the difference between a shallow copy and a deep copy of an object?
A shallow copy duplicates only the top level, so nested objects are still shared with the original. A deep copy duplicates every level, so the two are fully independent. Spread and Object.assign are shallow; structuredClone is deep.
BeginnerQuick answer1 min read - 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 - 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