JS: “in” Operator

By Xah Lee. Date: . Last updated: .
key in obj
Return true if property key key is obj's own property or if key is a property of some object in obj's Prototype Chain. (Both string and Symbol keys) Else, false.
// simple example
const xx = { "p1": 1 };
console.log("p1" in xx);
// in-operator on parent property

const x1 = { "p1": 1 };

// create a object x2, with parent x1
const x2 = Object.create(x1);

console.log("p1" in x2);
// the “in” operator works with symbol key
const ss = Symbol();
const x1 = { [ss]: 4 };
console.log(ss in x1);

🛑 WARNING: the syntax key in obj is not a part of the syntax for (key in obj) {body}. They look the same but don't have the same meaning. [see for-in Loop]

JavaScript, Check Property Existence

BUY ΣJS JavaScript in Depth