JavaScript Object Property Access and Existence

✅ JavaScript Output — Understanding Object Properties Clearly This morning’s code was: let user = {  name: "Veera" }; console.log(user.name); console.log(user.age); console.log("age" in user); console.log(user.hasOwnProperty("age")); 💡 Correct Output Veera undefined false false 🧠 Simple Explanation : 🔹 Line 1: console.log(user.name); The property name exists in the object. So JavaScript prints: Veera 🔹 Line 2: console.log(user.age); The property age does not exist in the object. When you access a missing property, JavaScript does not throw an error — it simply returns: undefined ✔ Missing property ≠ error. 🔹 Line 3: "age" in user The in operator checks: “Does this property exist in the object (or its prototype)?” Since age is not present anywhere, the result is: false 🔹 Line 4: user.hasOwnProperty("age") This checks: “Is this property defined directly on this object?” Again, age is not defined on user. So the result is: false 🎯 Key Takeaways : Accessing a missing property returns undefined "key" in object checks existence, not value hasOwnProperty() checks only direct properties undefined value ≠ property exists 📌 This distinction is very important when working with: APIs Forms Optional data 💬 Your Turn Did you know the difference between undefined, in, and hasOwnProperty? Comment “Clear now ✅” or “Learned today 🙌” #JavaScript #FrontendDevelopment #LearnJS #CodingInterview #Objects #TechWithVeera #WebDevelopment #100DaysOfCode

  • graphical user interface

To view or add a comment, sign in

Explore content categories