📘 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.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare 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