What is the least amount of code needed to do the following:
If an object exists and it has a required property and the property is not an empty string then set a variable to the value of the property else set the variable to a default string.
Assuming that the object variable can only be undefined or a valid object, it can never be a function, null, string or anything else. Also if the object has the required property it is a string and never anything else.
A solution to this maybe:
// obj is the object we are testing, prop is the name of the property, def is the default string
var result = def;
if (obj && obj[prop] && obj[prop].length) {
result = obj[prop];
}
Whether this is completely correct I am unsure.
But is there a shorter way?
Thanks,
AJ