How can I determine whether an object x has a defined property y, regardless of the value of x.y?
I’m currently using
if (typeof(x.y) !== 'undefined')
but that seems a bit clunky. Is there a better way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Object has property:
If you are testing for properties that are on the object itself (not a part of its prototype chain) you can use
.hasOwnProperty():Object or its prototype has a property:
You can use the
inoperator to test for properties that are inherited as well.