Learned a valuable lesson about JavaScript's delete operator in strict mode.

💡 𝗦𝗺𝗮𝗹𝗹 𝗠𝗶𝘀𝘁𝗮𝗸𝗲, 𝗕𝗶𝗴 𝗟𝗲𝘀𝘀𝗼𝗻 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁! Today, I hit an error that said: '𝘥𝘦𝘭𝘦𝘵𝘦' 𝘤𝘢𝘯𝘯𝘰𝘵 𝘣𝘦 𝘤𝘢𝘭𝘭𝘦𝘥 𝘰𝘯 𝘢𝘯 𝘪𝘥𝘦𝘯𝘵𝘪𝘧𝘪𝘦𝘳 𝘪𝘯 𝘴𝘵𝘳𝘪𝘤𝘵 𝘮𝘰𝘥𝘦. At first glance, it looked confusing — but it turned out to be a great reminder about how JavaScript actually works under the hood. In JavaScript, the 𝘥𝘦𝘭𝘦𝘵𝘦 operator is meant to remove object properties, not variables. ✅ delete obj.key — works perfectly ❌ delete variableName — throws an error (especially in strict mode) 𝗪𝗵𝘆? Because variables declared with let, const, or var are not object properties — they belong to the current scope. Trying to “delete” them breaks the language rules. 𝗟𝗲𝘀𝘀𝗼𝗻 𝗹𝗲𝗮𝗿𝗻𝗲𝗱: If you just want to clear a variable, use variable = null; and let JavaScript’s garbage collector do the rest. Sometimes the smallest syntax errors teach the biggest fundamentals! 🚀 #JavaScript #TypeScript #WebDevelopment #CodingTips #LearningEveryday

To view or add a comment, sign in

Explore content categories