How to Create Pure Objects in JavaScript

How to Create Pure Objects in JavaScript

Ferenc Almasi β€’ 2021 July 23 β€’ Read time 1 min read
  • twitter
  • facebook
JavaScript

When you create new objects in JavaScript β€” even if it's an empty object β€” it will have some extra properties and methods that are inherited from the global Object class. You can check what an empty object has in JavaScript by printing it to the console:

The inherited properties on an empty object in JavaScript

When talking about pure objects, we are referring to objects that don't inherit anything and therefore, don't have any additional properties upfront. To create one, we can simply use the Object.create method, and passing null to it:

Copied to clipboard!
const pure = Object.create(null);

Passing null to the method call sets what should be the prototype of the object, which in this case, is null.

How to Create Pure Objects in JavaScript
If you would like to see more webtips, follow @flowforfrank

50 JavaScript Interview Questions

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.