All falsey values in JavaScript -


what values in javascript 'falsey', meaning evaluate false in expressions if(value), value ? , !value?


there some discussions of purpose of falsey values on stack overflow already, no exhaustive complete answer listing falsey values are.

i couldn't find complete list on mdn javascript reference, , surprised find top results when looking complete, authoritative list of falsey values in javascript blog articles, of had obvious omissions (for example, nan), , none of had format stack overflow's comments or alternative answers added point out quirks, surprises, omissions, mistakes or caveats. so, seemed make sense make one.

falsey values in javascript

  • false
  • 0 , -0
  • "" , ''
  • null
  • undefined
  • nan
  • document.all - weird one, it's falsey object, typeof undefined. microsoft-proprietory function in ie before ie11, , added html spec "willful violation of javascript specification" sites written ie wouldn't break on trying access, example, document.all.something; it's falsy because if (document.all) used popular way detect ie, before conditional comments. see why `document.all` falsy? details

"falsey" means javascript's internal toboolean function returns false. toboolean underlies !value, value ? ... : ...; , if(value){...}else{...}.

for comparisons == these form 3 groups:

  • false, 0, -0, "", '' match each other ==
    • e.g. false == "" , '' == 0
  • null, undefined match ==
    • e.g. null == undefined undefined != false
    • it's worth mentioning while typeof null returns 'object', null not object, longstanding bug/quirk not fixed in order maintain compatibility. it's not true object, , objects truthy (except "wilful violation" document.all when javascript implemented in html)
  • nan doesn't match ==, not itself
    • e.g. nan != nan, nan != false, nan != null

only false matches false ===.


truthy values == false

"truthy" means javascript's internal toboolean function returns true. a quirk of javascript aware of: possible value truthy (toboolean returns true), == false.

you might think if( value && value==false ){ alert("huh?"); } logical impossibility couldn't happen, will, for:

  • "0" , '0' - they're non-empty strings, truthy, javascript's == matches numbers equivalent strings (e.g. 42 == "42"). since 0 == false, if "0"==0, "0"==false.
  • new number(0) , new boolean(false) - they're objects, truthy, == sees values, == false.
  • 0 .toexponential(); - object numerical value equivalent 0
  • any similar constructions give false-equaling value wrapped in type truthy
  • [], [[]] , [0] (thanks cloudfeet javascript equality table link)

some more truthy values

these few values people might expect falsey, truthy.

  • -1 , non-zero negative numbers
  • ' ', " " , non-empty strings, if they're whitespace
  • anything typeof, returns non-empty string, example:

  • any object (keep in mind null isn't object), including:

    • {}
    • []
    • function(){} (any variable defined function, including empty functions)
    • any regular expression
    • anything created new (including otherwise-falsey values created new, see above)

true, 1, "1" , [1] return true when compared each other ==.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -