Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Last active April 15, 2020 19:46
Show Gist options
  • Save thawkin3/07e450fa5e7bb7381369246fb52f647f to your computer and use it in GitHub Desktop.
Save thawkin3/07e450fa5e7bb7381369246fb52f647f to your computer and use it in GitHub Desktop.
Finding values that are not null or undefined without nullish coalescing
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
@thawkin3
Copy link
Author

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