I am looking to determine if an object contains a specific field with a value, and return a default value if it doesn't.
function checkValue(object, defaultValue) {
if (!object) {
console.log(defaultValue);
} else {
console.log(object);
}
}
let testObject;
checkValue(testObject, 'this is the default value')
However, I want this function to handle checking values without requiring them to be passed explicitly...
If I try to pass:
testObject.field1.value
This will result in an error:
TypeError: Cannot read property 'field1' of undefined
Therefore, I need a way to pass any object and evaluate its value without using ternary ?:
operators.