Skip to solution
hardFrontend

What does the 'new' keyword do?

311 views
01

Understand the problem

Question presented to candidate: "If a constructor function explicitly does return { y: 2 } at the end, what does new SomeConstructor() actually give you back — the newly created instance, or that returned object?"

What a strong answer should cover:

  • 📌 Interview term: what new actually does, step by step — creates a brand-new, empty object; links that object's prototype to the constructor function's own .prototype; calls the constructor function with this bound to that new object; and, by default, returns that object — verified directly, each step confirmed with a real constructor and prototype method.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: if the constructor explicitly returns an OBJECT, new genuinely returns THAT object instead of the newly-created this — the real object literal { y: 2 } genuinely wins, and the properties set on this (like x) are genuinely discarded/inaccessible from the result.
  • 📌 Interview term: an explicit PRIMITIVE return is ignored — verified directly, in real, sharp contrast: if the constructor instead returns a primitive (a number, string, boolean), new genuinely ignores it entirely, still returning the real this object as normal.
  • 📌 Interview term: new.target — verified directly: a function can detect whether it was genuinely invoked with new at all via new.target, which is genuinely undefined for a plain call and genuinely set for a new call.
  • A precise answer names that calling a regular function WITHOUT new genuinely does not create any instance at all (this follows the normal call-site rules covered in this bank's own call/apply/bind questions), and that calling an ARROW function WITH new genuinely throws a real TypeError, since arrow functions are deliberately non-constructible.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's own explicit-object-return scenario is the strong signal, since it is the single most commonly-missed new detail.

Code / implementation expected: Yes — the direct object-return-vs-primitive-return contrast is the clearest, most convincing demonstration of new's real, precise behavior.

new
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals/OOP interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every constructor-return edge case below was actually run in Node.

1. Why This Ev

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: new returns this by default, but an explicit object return from the constructor genuinely wins, while a primitive return is genuinely ignored
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 155 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track