JS: in (operator) 👎
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 aa = { "p1": 1 }; // create a object bb, with parent aa const bb = Object.create(aa); console.log("p1" in bb); // true
// the in operator works with symbol key const xx = Symbol(); const yy = { [xx]: 4 }; console.log(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]