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.

const xx = { "pp": 1 };
console.assert(("pp" in xx) === true);
// in-operator on parent property

const xdad = { "pp": 1 };

// create a object xson, with parent xdad
const xson = Object.create(xdad);

console.assert(("pp" in xson) === true);
// the in operator works with symbol key
const xx = Symbol();
const yy = { [xx]: 4 };
console.assert(xx in yy);

🛑 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. Access Properties