Sep 4, 2018 · 1 min read
This will work in most situations, but you must be careful of valid, yet falsy values:
function getValue(name) {
const names = {
'Zero': 0,
'Null': null,
'Undefined': undefined,
'False': false,
'NaN': NaN,
'EmptyString': ''
}; return names[name] || 'Unknown';
}getValue('Zero') //=> "Unknown"
getValue('Null') //=> "Unknown"
getValue('Undefined') //=> "Unknown"
getValue('False') //=> "Unknown"
getValue('NaN') //=> "Unknown"
getValue('EmptyString') //=> "Unknown"
Also, I have to disagree on always using ternary instead of if, they tend to turn into brainf**k after some nesting depth, no matter what. :)
This is just Familiarity Bias. You have been exposed to ternaries less, and because you have see it less, it appears as foreign.
If you have used ternaries your whole life, you could use the same exact argument for the non-ternary version.
