Sub topic 6
Checking Whether a Property Exists
This chapter assumes you can create objects with literals, read properties with dot and bracket notation, and add or delete properties. You will learn how to tell a missing property apart from one that holds undefined, how to separate own properties from inherited ones, and how to read nested values safely.
Why `obj.x === undefined` Is Not Enough
When you read a property that does not exist, JavaScript returns undefined. A property can also exist and hold the value undefined. A plain comparison cannot tell these two cases apart:
const settings = { theme: undefined };
console.log(settings.theme === undefined); // true (the property exists)
console.log(settings.font === undefined); // true (the property is missing)If the difference matters, for example when you want to know whether a caller passed an option at all, you need a check that asks about the key itself instead of its value.
The `in` Operator
key in obj returns true if the object has the property, whatever its value is. It also looks up the prototype chain, so inherited properties count. The prototype chain is the link from one object to another that it inherits from, and it gets its own chapter later.
console.log("theme" in settings); // true
console.log("font" in settings); // false
console.log("toString" in settings); // true, inherited from Object.prototypeThe last line catches many people out. Every ordinary object inherits toString, hasOwnProperty, and other methods, so in reports them as present.
`Object.hasOwn` for Own Properties Only
Object.hasOwn(obj, key) returns true only when the property is defined directly on the object. It ignores inherited properties.
const proto = { inherited: true };
const obj = Object.create(proto);
obj.own = undefined;
console.log("own" in obj); // true
console.log("inherited" in obj); // true
console.log(Object.hasOwn(obj, "own")); // true
console.log(Object.hasOwn(obj, "inherited")); // false
delete obj.own;
console.log("own" in obj); // falsedelete removes the key entirely. After the delete, both checks report the property as missing. Setting obj.own = undefined would have kept it.
Optional Chaining for Nested Reads
Reading a property of undefined or null throws a TypeError. The optional chaining operator ?. stops evaluating and returns undefined when the value on its left is null or undefined.
const user = { profile: null, greet() { return "hi"; } };
console.log(user.profile?.name); // undefined, no error
console.log(user.address?.city); // undefined
console.log(user["profile"]?.name); // undefined, works with brackets too
console.log(user.greet?.()); // "hi"
console.log(user.wave?.()); // undefined, method is missingOptional chaining protects you from crashes. It does not tell you whether a property exists, because a missing property and one holding undefined give the same result. Pair it with ?? to supply a fallback: user.profile?.name ?? "Anonymous".
Choosing a Check
| Check | Inherited counts | Present with `undefined` value | Throws on `null` base |
|---|---|---|---|
| `obj.k !== undefined` | yes | reports missing | yes |
| `"k" in obj` | yes | reports present | yes |
| `Object.hasOwn(obj, "k")` | no | reports present | yes |
| `obj?.k` | yes | returns `undefined` | no |
Common Errors
- Using
inon a lookup table built from user input. A key like"constructor"matches an inherited property. UseObject.hasOwn, or build the table withObject.create(null)or aMap. - Using a truthiness check such as
if (obj.count). It treats0,"", andfalseas missing. - Using
inwith a primitive on the right, as in"length" in "abc". This throws aTypeError.
Exercise
Given const o = Object.create({ a: 1 }); o.b = undefined;, what do "a" in o, Object.hasOwn(o, "a"), Object.hasOwn(o, "b"), and o.c?.d return?
Show answer
true (a is inherited), false (a is not own), true (b exists even though its value is undefined), and undefined (o.c is undefined, so the chain stops without throwing).
Exercise
A function receives options and should use a default timeout only when the caller did not pass a timeout key at all, even if they passed timeout: undefined on purpose. Which check fits, and why not options.timeout ?? 1000?
Show answer
Use Object.hasOwn(options, "timeout"). The ?? operator treats an explicit undefined the same as a missing key, so it cannot tell whether the caller passed the key. Object.hasOwn checks the key itself and also ignores inherited properties.