I'm trying to understand how the third line in this code snippet (the assignment value = f in the condition of the conditional expression) is working:
someProp(propName, f) {
let prop = this._props && this._props[propName], value
if (prop != null && (value = f ? f(prop) : prop)) return value
...
}
In this example, value is undefined at the console.log(value) statement:
let f = 42;
value = f ? console.log('true') : console.log('false');
console.log(value);
It seems to me that the value variable returned on the third line of the first snippet would always be undefined. What am I missing? The snippet is from production code in an open source project I'm studying.
Also, this._props is an object. Why is the author using if (prop != null && ... to test if the props object exists, rather than just something like if (prop && ... ? And am I right in thinking that this is typical code of more experienced developers, or would others write it differently?
value = f ? console.log('true') : console.log('false');in this casevaluewill always beundefinedbecause it will be set to the return value ofconsole.log()which is alwaysundefined.value = f ? f(prop) : prop)in this case, iffis falsey, thenvalue = prop, iff`` is truthy (based on context, I suspect a callback), thenvalue` will be what is returned byf(prop)