📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
typeof
operator.console.log(typeof 42);
// expected output: "number"
console.log(typeof 'blubber');
// expected output: "string"
console.log(typeof true);
// expected output: "boolean"
console.log(typeof declaredButUndefinedVariable);
// expected output: "undefined";
How to check if a JavaScript object property is undefined?
// using Object Literals
var user = {
emailId : 'ramesh@gmail.com',
age : 29,
getFullName : function (){
return user.firstName + " " + user.lastName;
}
};
function isUndefined(user) {
if(typeof user.firstName === 'undefined'){
console.log("User first name is undefined");
}
if(typeof user.lastName === 'undefined'){
console.log("User last name is undefined");
}
}
isUndefined(user);
User first name is undefined
User last name is undefined
Complete code
// using Object Literals
var user = {
emailId : 'ramesh@gmail.com',
age : 29,
getFullName : function (){
return user.firstName + " " + user.lastName;
}
};
function isUndefined(user) {
if(typeof user.firstName === 'undefined'){
console.log("User first name is undefined");
}
if(typeof user.lastName === 'undefined'){
console.log("User last name is undefined");
}
}
isUndefined(user);
User first name is undefined
User last name is undefined
Comments
Post a Comment
Leave Comment