Checking if a key exists in a JavaScript object: 3 ways for beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Checking if a key exists in a JavaScript object: a breakdown for beginners

\n\n

Have you ever tried to get a value from a JavaScript object and received a mysterious undefined in response? A familiar situation, right? Especially when working with data from an API, configs, or just large structures. Today we will finally figure out how to properly check if a key exists in an object. This is one of the most popular questions on Stack Overflow — and for good reason.

\n\n

Imagine that an object is a cabinet with drawers. You want to know if there is a drawer labeled "keys". You could just pull the handle (and get an error if the drawer doesn't exist), or you could first look and make sure. In JavaScript, we have three reliable ways to do this. Let's go!

\n\n

Why even check for a key's existence?

\n\n

Let's say you are writing a shopping list application. You have a cart object that can contain items with quantities:

\n\n
const cart = {\n  apples: 3,\n  bananas: 5\n};\n\n// We want to add pears, but don't know if they already exist\nfunction addItem(item, quantity) {\n  if (cart[item] !== undefined) {\n    cart[item] += quantity;\n  } else {\n    cart[item] = quantity;\n  }\n}
\n\n

This code will work, but it has pitfalls. What if the key's value is 0 or null? Then the check for undefined will give a false result. So let's look at professional approaches.

\n\n

Method 1: The in operator — the universal soldier

\n\n

The in operator checks if a property exists in an object, including inherited properties. This is the most reliable way when you need to know if a key exists at all.

\n\n
const user = {\n  name: 'Anna',\n  age: 25,\n  isAdmin: false\n};\n\nconsole.log('name' in user);    // true\nconsole.log('age' in user);     // true\nconsole.log('salary' in user);  // false\nconsole.log('toString' in user); // true (inherited from Object)
\n\n

Pros: works even if the value is null, undefined, or false. Checks the entire prototype chain.

\n\n

Cons: may find inherited properties that you don't need. For example, toString or hasOwnProperty.

\n\n

For most cases, in is the ideal choice. Especially when you are working with regular dictionary-like objects.

\n\n

Method 2: hasOwnProperty() — only your own keys

\n\n

If you need to check only the object's own properties (not inherited ones), use the hasOwnProperty method. This is a standard method of all objects in JavaScript.

\n\n
const book = {\n  title: 'JavaScript for Dummies',\n  pages: 300\n};\n\nconsole.log(book.hasOwnProperty('title'));   // true\nconsole.log(book.hasOwnProperty('pages'));   // true\nconsole.log(book.hasOwnProperty('toString')); // false (not an own property)\nconsole.log(book.hasOwnProperty('author'));   // false
\n\n

Note: hasOwnProperty does not check inherited properties. This makes it indispensable when you create objects using classes or prototypes.

\n\n

But there is a nuance! If your object was created using Object.create(null), it will not have the hasOwnProperty method. What to do? Use a safe call:

\n\n
const dict = Object.create(null);\ndict.key1 = 'value1';\n\n// This won't work:\n// console.log(dict.hasOwnProperty('key1')); // TypeError\n\n// But this will:\nconsole.log(Object.prototype.hasOwnProperty.call(dict, 'key1')); // true
\n\n

Modern syntax allows using Object.hasOwn() — a static method added in ES2022. It solves this problem elegantly:

\n\n
console.log(Object.hasOwn(dict, 'key1')); // true
\n\n

I recommend using Obj

Blogs

Book Recommendations