I have to check if a object is undefined but when i do
typeof myUnexistingObject.myUnexistingValue == 'undefined'
i get this error
Uncaught ReferenceError: myUnexistingObject is not defined
so, how can I check for undefined obects or properties?
You must check for each potentially defined property before using it:
function checkUnexistingObject(myUnexistingObject) {
if (myUnexistingObject !== undefined) {
if (myUnexistingObject.otherObject !== undefined) {
console.log("All is well");
}
}
}
checkUnexistingObject({});
checkUnexistingObject({otherObject: "hey"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With