Skip to content

Latest commit

 

History

History
22 lines (16 loc) · 774 Bytes

File metadata and controls

22 lines (16 loc) · 774 Bytes

in Operator Patterns are Rare

It's rare (in my experience) to see the in operator used in JS — or at least much less common than in other languages. Normally we'll see something along the lines of:

typeof window !== "undefined"

vs.

"window" in globalThis

I've learned that this is because they're not equivalent, and that using the latter pattern can lead to unanticipated results in vanilla JS, particularly when undefined is involved:

a = {b: undefined}
"b" in a // returns True
typeof a.b !== "undefined" // returns False

But! It's possible to begin leveraging in more safely in TypeScript with type guards in operator narrowing.