What Does the “new” Keyword do in JavaScript under the hood?

Let’s talk about the new keyword in JavaScript. It’s like the magic wand that makes constructor functions do their thing. But what’s really going on behind the scenes?

Pulls Out a Fresh Object
The first thing new does is whip up a shiny, empty objec…


This content originally appeared on DEV Community and was authored by mmvergara

Let’s talk about the new keyword in JavaScript. It’s like the magic wand that makes constructor functions do their thing. But what’s really going on behind the scenes?

  1. Pulls Out a Fresh Object

    The first thing new does is whip up a shiny, empty object. Think of it as a blank canvas waiting to be painted on.

  2. Links It Up

    That blank object? It gets hooked up to the prototype of the constructor function. Now it knows who its “parent” is, like getting added to a cool family tree.

   obj.__proto__ = ConstructorFunction.prototype;
  1. Hands Over the Keys Inside the constructor, this becomes the new object. You can now slap on some properties and methods like you’re decorating your new room.
   ConstructorFunction.call(obj);
  1. Goes With the Flow Finally, it checks if your constructor is returning something specific. If not, it shrugs and hands back the new object it made earlier.

Example time:

function Animal(type) {
  this.type = type;
}

const cat = new Animal('cat');
console.log(cat.type); // cat

Without new, all this cool stuff doesn’t happen—this points to the wrong place, and the prototype chain? Totally busted. So yeah, new is like your friendly helper, making sure everything runs smoothly when you’re building stuff.


This content originally appeared on DEV Community and was authored by mmvergara


Print Share Comment Cite Upload Translate Updates
APA

mmvergara | Sciencx (2025-01-04T23:10:56+00:00) What Does the “new” Keyword do in JavaScript under the hood?. Retrieved from https://www.scien.cx/2025/01/04/what-does-the-new-keyword-do-in-javascript-under-the-hood/

MLA
" » What Does the “new” Keyword do in JavaScript under the hood?." mmvergara | Sciencx - Saturday January 4, 2025, https://www.scien.cx/2025/01/04/what-does-the-new-keyword-do-in-javascript-under-the-hood/
HARVARD
mmvergara | Sciencx Saturday January 4, 2025 » What Does the “new” Keyword do in JavaScript under the hood?., viewed ,<https://www.scien.cx/2025/01/04/what-does-the-new-keyword-do-in-javascript-under-the-hood/>
VANCOUVER
mmvergara | Sciencx - » What Does the “new” Keyword do in JavaScript under the hood?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/04/what-does-the-new-keyword-do-in-javascript-under-the-hood/
CHICAGO
" » What Does the “new” Keyword do in JavaScript under the hood?." mmvergara | Sciencx - Accessed . https://www.scien.cx/2025/01/04/what-does-the-new-keyword-do-in-javascript-under-the-hood/
IEEE
" » What Does the “new” Keyword do in JavaScript under the hood?." mmvergara | Sciencx [Online]. Available: https://www.scien.cx/2025/01/04/what-does-the-new-keyword-do-in-javascript-under-the-hood/. [Accessed: ]
rf:citation
» What Does the “new” Keyword do in JavaScript under the hood? | mmvergara | Sciencx | https://www.scien.cx/2025/01/04/what-does-the-new-keyword-do-in-javascript-under-the-hood/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.