JavaScript Property Gotcha: The 'in' Operator

Ever tried reading a property that doesn’t exist in JavaScript? No crash. No error. Just… undefined 😄 Most of us do this: if (user.noSuchProperty === undefined) { ... } Works 99% of the time. Until it doesn’t. let obj = { test: undefined }; console.log(obj.test); // undefined console.log("test" in obj); // true ← surprise! The property exists — it just happens to hold undefined. That tiny difference is exactly why the in operator exists. Have you ever been bitten by this gotcha? Or do you still prefer === undefined in everyday code? #JavaScript #CodingTips #WebDevelopment

To view or add a comment, sign in

Explore content categories