Joel Thoms
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.

    Joel Thoms

    Written by

    Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade