Basics

What is a prototype in JavaScript — and why an empty object isn't empty

Illustration: a small box sits on a bigger one, which sits on an even bigger one, each holding something

You're building a word counter. Classic: an empty object, loop over the words, add one.

const counts = {}
for (const word of text.split(" ")) {
  counts[word] = (counts[word] || 0) + 1
}

On normal text it works. Then the word "constructor" shows up. And instead of a number you get:

constructor: "function Object() { [native code] }1"

Where did a function come from in an empty object? Here's the surprise: an empty object in JavaScript isn't empty. It has a prototype. Let's see what that is and why it sometimes bites.

What a prototype is

A prototype is an object's hidden "parent", where JavaScript looks for properties the object itself doesn't have.

You write {}. Zero own properties. But try:

const box = {}
box.toString // a function!

Nobody put toString into box. It was found on the parent — the shared prototype of all objects, Object.prototype. That's where toString, hasOwnProperty, constructor and a dozen more live.

How the lookup chain works

When you write box.something, JavaScript does this:

  1. Looks for something in box itself.
  2. Not found — goes to box's prototype.
  3. Not there either — to the prototype's prototype.
  4. Reached the end of the chain — returns undefined.

That's the prototype chain. Like boxes stacked on top of each other: you look in the top one, then the one below.

Arrays work the same way. The map method isn't stored in every array you make. It lives once in Array.prototype, and every array finds it through the chain. Efficient.

Hence an important difference between two checks:

"toString" in {}              // true — found on the parent
Object.hasOwn({}, "toString") // false — no own property

in looks along the whole chain. Object.hasOwn looks only at the object itself.

What broke in the word counter

Now it's clear. The word "constructor":

  1. counts["constructor"] — no own one, but the parent has it! It's the Object function.
  2. function || 0 — a function counts as "truthy", so the function stays.
  3. function + 1 — JavaScript glues its source text to a one. You get a string.

The word "toString" breaks the counter the same way. If the words come from users, it's only a matter of time.

Three ways to fix it:

  • Use a Map instead of an object. A Map has only your keys, no surprises from a parent. For dictionaries with outside keys, it's the best choice.
  • An object with no prototype: Object.create(null). Truly empty.
  • Check own keys: Object.hasOwn(counts, word) ? counts[word] + 1 : 1.

The dangerous side: __proto__ in someone else's JSON

There's an attack called prototype pollution. Here's how it works.

Objects have a special __proto__ key — it gets you to the parent. Say you merge user settings into yours with a hand-written deep-merge function. And an attacker sends this JSON:

{ "__proto__": { "isAdmin": true } }

A naive merge walks into the __proto__ key and writes isAdmin straight into Object.prototype. After that, for every object in the program, ({}).isAdmin is true. Including your permission check.

This isn't theory: holes like this were found in popular libraries, for example lodash (CVE-2019-10744). How to protect yourself:

  • don't write deep merge yourself — use a well-tested library, kept up to date;
  • when walking keys, skip __proto__, constructor and prototype;
  • validate incoming data against a schema before merging.

If you're getting a project ready for launch, this belongs on your security checklist.

Where you'll meet prototypes

  • Classes. class in JavaScript is prototypes in a neat suit. Class methods live on the prototype, not in each instance.
  • "Borrowed" methods on strings and numbers. "hello".toUpperCase() works because the method lives in String.prototype.
  • for...in oddities. It also walks inherited enumerable properties. If some library added a method to Array.prototype, it will pop up in your loop.

How is prototype different from __proto__?

An object's __proto__ points to its parent. prototype exists on constructor functions and classes: it's the object that becomes the parent of everything they create.

Do I need to understand prototypes if I write classes?

Not deeply. But it's worth remembering one thing: methods live on the prototype. That explains the surprises when methods disappear after copying an object.

What is Object.create(null)?

It's an object with no parent at all. No toString, no hasOwnProperty — only what you put in. Great as a dictionary, but you can't, for example, glue it directly onto a string.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →