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
false0,-0"",''nullundefinednandocument.all- weird one, it's falsey object,typeofundefined. 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 becauseif (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
- e.g.
null, undefinedmatch==- e.g.
null == undefinedundefined != false - it's worth mentioning while
typeof nullreturns'object',nullnot object, longstanding bug/quirk not fixed in order maintain compatibility. it's not true object, , objects truthy (except "wilful violation"document.allwhen javascript implemented in html)
- e.g.
nandoesn't match==, not itself- e.g.
nan != nan,nan != false,nan != null
- e.g.
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"). since0 == false, if"0"==0,"0"==false.new number(0),new boolean(false)- they're objects, truthy,==sees values,== false.0 .toexponential();- object numerical value equivalent0- 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 whitespaceanything
typeof, returns non-empty string, example:typeof null(returns string'object'due longstanding bug/quirk)typeof undefined(returns string'undefined')
any object (keep in mind
nullisn't object), including:{}[]function(){}(any variable defined function, including empty functions)- any regular expression
- anything created
new(including otherwise-falsey values creatednew, see above)
true, 1, "1" , [1] return true when compared each other ==.
Comments
Post a Comment