What does the `new` keyword do under the hood?
Quick answer
Creates an empty object, links it to the constructor's prototype, runs the constructor with `this` bound to it, and returns it.
In detail
`new Fn(args)` does four things: (1) create a fresh object, (2) set its prototype to Fn.prototype, (3) call Fn with `this` set to the new object, (4) return that object (unless the constructor returns its own object). You can replicate it to prove understanding.
function myNew(Fn, ...args) {
const obj = Object.create(Fn.prototype);
const res = Fn.apply(obj, args);
return res instanceof Object ? res : obj;
}Why interviewers ask this: A deeper product-company question on object creation.
This is 1 of 118+ questions in the JavaScript Interview Kit
Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.
⚡ Get the JavaScript Interview Kit → ₹299