Featurecore
Querying object properties
Also called in operator, Object.hasOwn, hasOwnProperty, checking if a property exists, property existence test, optional chaining, undefined properties.
JavaScript tests for a property with in, which also searches the prototype chain, or Object.hasOwn, which checks own properties only. Optional chaining reads a property that may be missing.
- Language
- JavaScript
Synopsis
const user = { name: "Ada", nickname: undefined };
console.log("name" in user, "toString" in user, "age" in user);
console.log(Object.hasOwn(user, "name"), Object.hasOwn(user, "toString"));
console.log(user.nickname === undefined, "nickname" in user);
console.log(user.address?.city);
true true false
true false
true true
undefined
| Test | Own properties | Inherited properties | A property set to undefined |
|---|---|---|---|
key in obj |
true | true | true |
Object.hasOwn(obj, key) |
true | false | true |
obj.key !== undefined |
true | true | false |
obj.propertyIsEnumerable(key) |
true if enumerable | false | true if enumerable |
Semantics
r1. "The in operator returns true if the specified property is in the specified object or its prototype chain."1 In ECMA-262 it calls HasProperty, which "is used to determine whether an object has a property with the specified property key. The property may be either own or inherited."2 Example: basics.
r2. Object.hasOwn "returns true if the specified object has the indicated property as its own property. If the property is inherited, or does not exist, the method returns false ."3 It uses HasOwnProperty, which "is used to determine whether an object has an own property with the specified property key."2 "Object.hasOwn() is intended as a replacement for Object.prototype.hasOwnProperty() ."3 Example: basics.
r3. Reading a missing property gives undefined, but so does reading a property whose value is undefined. MDN: "A property may be present in an object but have value undefined ." "To make in return false after a property is added, use the delete operator instead of setting that property's value to undefined ."1 Example: undef.
r4. The right side of in must be an object. The steps say "If rVal is not an Object, throw a TypeError exception."4 The steps of Object.hasOwn begin "Let obj be ? ToObject(O).", so it works on a string.5 Example: primitive.
r5. For arrays, in tests an index, not a value. "The in operator cannot be used to search for values in other collections. To test if a certain value exists in an array, use Array.prototype.includes() ."1 "The in operator will return false for empty array slots, even if accessing it directly returns undefined ."1 Example: sparse.
r6. Optional chaining reads through a value that may be missing. "If the object accessed or function called using this operator is undefined or null , the expression short circuits and evaluates to undefined instead of throwing an error."6 Example: optional.
Examples
A property set to undefined is still there (rule r3).
const o = { a: undefined };
console.log("a" in o, o.a, o.b);
o.a = 1;
delete o.a;
console.log("a" in o);
true undefined undefined
false
Empty slots in an array (rule r5).
const empties = new Array(3);
const filled = new Array(3).fill(undefined);
console.log(empties[2], 2 in empties, 2 in filled);
undefined false true
Reading a path that may be missing (rule r6).
const order = { customer: { name: "Ada" } };
console.log(order.customer?.name, order.shipping?.city, order.notify?.());
try {
order.shipping.city;
} catch (e) {
console.log(e.name);
}
Ada undefined undefined
TypeError
Versions
| Feature | Edition |
|---|---|
optional chaining ?. |
ECMAScript 20207 |
Object.hasOwn |
ECMAScript 20227 |
The TC39 list of finished proposals gives 2020 for Optional Chaining and 2022 for Accessible Object.prototype.hasOwnProperty, the proposal that added Object.hasOwn.7
Pitfalls
Testing a property by its truth value
This code runs but gives a wrong answer on purpose.
const stock = { apples: 0 };
console.log(stock.apples ? "have" : "missing");
console.log("apples" in stock, Object.hasOwn(stock, "apples"));
missing
true true
0 is falsy, so the test reports a property that exists as missing. MDN gives the same warning for && chains, where a falsy value "that's not null or undefined , such as 0" stops the chain.6 Use in or Object.hasOwn to ask whether the property is there (rules r1 and r2).
Using in on a primitive
This code runs but gives a wrong answer on purpose.
try {
console.log("length" in "abc");
} catch (e) {
console.log(e.name);
}
console.log("length" in new String("abc"), Object.hasOwn("abc", "length"));
TypeError
true true
A string literal is not an object, so in throws (rule r4).
Using in to search an array for a value
This code runs but gives a wrong answer on purpose.
const trees = ["redwood", "bay"];
console.log("bay" in trees, 1 in trees, trees.includes("bay"));
false true true
"bay" in trees asks for a property named bay. Use includes to search the values (rule r5).
Comparison
Python's hasattr: "The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)"8 Like in, it finds attributes from the class too. Go has no general property test, but a map index in the special form v, ok := a[x] "yields an additional untyped boolean value. The value of ok is true if the key x is present in the map, and false otherwise."9 JavaScript has both kinds of test: in for the whole prototype chain and Object.hasOwn for the object alone (rules r1 and r2).
References
- MDN Web Docs, JavaScript Reference, in, the introduction; Description; Using the in operator with deleted or undefined properties
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 7, 7.3.11 HasProperty; 7.3.12 HasOwnProperty
- MDN Web Docs, JavaScript Reference, Object.hasOwn(), the introduction; Description
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 13, 13.10.1 Runtime Semantics, Evaluation
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 20, 20.1.2.14 Object.hasOwn
- MDN Web Docs, JavaScript Reference, Optional chaining (?.), the introduction; Description
- TC39, Finished Proposals, Optional Chaining, Accessible Object.prototype.hasOwnProperty, Expected Publication Year
- The Python Standard Library, Python 3.14, Built-in Functions, hasattr
- The Go Programming Language Specification, language version go1.27, Index expressions
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 23PBFE Edit this page History Report an error Markdown