I want to check using function by passing variable into parameter whether that property exists in the object or not.I tried all three mostly used ways to check the existing properties of an object,but still i'm getting the output as undefined
.Can anyone tell me where i am wrong ?.
var obj=[
{
"firstName": "James",
"lastName": "Bond"
}];
function propExists(prop)
{
//I tried #1
if(obj.hasOwnProperty(prop)===false)
{
return "Property doesn't exist";
}
//I tried #2
if(!(prop in obj))
{
return "Property doesn't exist";
}
//I tried #3
if("undefined" === typeof(obj[prop]))
{
return "Property doesn't exist";
}
}
console.log(propExists("Date of birth"));