Logical operators in Javascript do not always return boolean result
Given a variable myObj in Javascript, evaluating myObj.child.name || ‘Kid’ will cause an error if myObj.child does not exist. In comparison, in PHP, $myObj->child->name ?? ‘Kid’ would just yield “Kid” with no errors. One way would be to write the expression let result = (myObj && myObj.child && myObj.child.name) ? myObj.child.name : ‘Kid’;. Another way …
Continue reading “Logical operators in Javascript do not always return boolean result”