Course contentsShow
← JavaScript
Lesson 723 of 2,72317. Prototypes and InheritanceFree lesson

Object.create(null) for Property Maps

Understand why Object.create(null) creates truly empty objects without inherited properties like toString.

Object.create(null) for Property Maps

What you'll learn: How to create truly empty objects without inherited properties for safe property storage.

The Problem with Regular Objects

When you create a normal object with {} or new Object(), it inherits properties from Object.prototype. This means your "empty" object already has properties like toString, hasOwnProperty, and constructor:

const regularObj = {};
console.log(regularObj.toString); // [Function: toString]
console.log(regularObj.hasOwnProperty); // [Function: hasOwnProperty]

Creating Truly Empty Objects

Object.create(null) creates an object with no prototype chain at all—it doesn't inherit anything:

const emptyObj = Object.create(null);
console.log(emptyObj.toString); // undefined
console.log(emptyObj.hasOwnProperty); // undefined
console.log(Object.getPrototypeOf(emptyObj)); // null

Why This Matters for Property Maps

When using objects as dictionaries or maps to store key-value pairs, inherited properties can cause unexpected bugs:

const userInput = "toString"; // Imagine this comes from user input

const regularMap = {};
const safeMap = Object.create(null);

regularMap[userInput] = "value";
safeMap[userInput] = "value";

console.log(regularMap.toString); // "value" — overwrote inherited method!
console.log(safeMap.toString); // "value" — just a property, no conflict

// Checking if a key exists
console.log("toString" in regularMap); // true (even if not set!)
console.log("toString" in safeMap); // true only if explicitly set

Think of it this way: Regular objects are like houses with inherited furniture—Object.create(null) gives you a completely bare room where only what you put in exists.

Key Takeaway: Use Object.create(null) when creating objects purely for property storage (maps/dictionaries) to avoid conflicts with inherited properties from Object.prototype.