Skip to the solution
HardFrontend6 min read

What does the 'new' keyword do?

311 views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 155 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How would you implement Symbol.asyncIterator on a class to transparently page through a paginated API with for await...of?

Continue