Skip to content

Instantly share code, notes, and snippets.

@upangka
Created December 31, 2019 02:46
Show Gist options
  • Save upangka/78138734e61bad1046955b72bc0730cd to your computer and use it in GitHub Desktop.
Save upangka/78138734e61bad1046955b72bc0730cd to your computer and use it in GitHub Desktop.
javascript check for null ,undefined but not include 0

背景在检验时,当值为null,和undefined,0,其boolean值都是false,但是我们需要0时校验为true.

let foo = 0
// check null,undefined not include 0
const f = (arg)=>{ return !arg && arg !== 0}
console.log(f(foo)) // false
foo = void 0 // undefined
console.log(f(foo)) // true
foo = null
console.log(f(foo)) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment