Last active
April 15, 2020 19:46
-
-
Save thawkin3/07e450fa5e7bb7381369246fb52f647f to your computer and use it in GitHub Desktop.
Finding values that are not null or undefined without nullish coalescing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useCoolFeature1 = true | |
const useCoolFeature2 = false | |
const useCoolFeature3 = undefined | |
const useCoolFeature4 = null | |
const getUserFeaturePreference = (featurePreference) => { | |
if (featurePreference || featurePreference === false) { | |
return featurePreference | |
} | |
return true | |
} | |
getUserFeaturePreference(useCoolFeature1) // true | |
getUserFeaturePreference(useCoolFeature2) // false | |
getUserFeaturePreference(useCoolFeature3) // true | |
getUserFeaturePreference(useCoolFeature4) // true |
Your suggestion is indeed simpler for the case of checking for a boolean. This example is a bit contrived, but for the purpose of demonstrating the differences between this example and the nullish coalescing operator here (https://gist.github.com/thawkin3/8318366aac732febe5e03b8cc460e1c5), the example should suffice.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find this simpler:
if (typeof featurePreference === 'boolean') {