The 7 falsy values are: 0 , 0n , null , undefined , false , NaN , and "" .
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .
Values not on the list of falsy values in JavaScript are called truthy values and include the empty array [] or the empty object {} . This means almost everything evaluates to true in JavaScript — any object and almost all primitive values, everything but the falsy values.
Truthy values are values that evaluate to True in a boolean context. Falsy values are values that evaluate to False in a boolean context. Falsy values include empty sequences (lists, tuples, strings, dictionaries, sets), zero in every numeric type, None , and False .
false
Number
type: 0
and also -0
, 0.0
, and hex form 0x0
(thanks RBT)BigInt
type: 0n
and 0x0n
(new in 2020, thanks GetMeARemoteJob)""
, ''
and ``
- strings of length 0null
undefined
NaN
document.all
(in HTML browsers only)
document.all
is a falsey object, with typeof
as undefined
. It was a Microsoft-proprietory function in IE before IE11, and was added to the HTML spec as a "willful violation of the JavaScript specification" so that sites written for IE wouldn't break on trying to access, for example, document.all.something
; it's falsy because if (document.all)
used to be a popular way to detect IE, before conditional comments. See Why is document.all falsy? for details"Falsey" simply means that JavaScript's internal ToBoolean
function returns false
. ToBoolean
underlies !value
, value ? ... : ...;
and if (value)
. Here's its official specification (2020 working draft) (the only changes since the very first ECMAscript specification in 1997 are the addition of ES6's Symbols, which are always truthy, and BigInt
, mentioned above:
Argument type Result Undefined Return false
.Null Return false
.Boolean Return argument. Number If argument is +0
,-0
, orNaN
, returnfalse
; otherwise returntrue
.String If argument is the empty String
(its length is zero), returnfalse
; otherwise returntrue
.BigInt If argument is 0n
, returnfalse
; otherwise returntrue
.Symbol Return true
.Object Return true
.
==
(loose equality)It's worth talking about falsy values' loose comparisons with ==
, which uses ToNumber()
and can cause some confusion due to the underlying differences. They effectively form three groups:
false, 0, -0, "", ''
all match each other with ==
false == ""
, '' == 0
and therefore 4/2 - 2 == 'some string'.slice(11);
null, undefined
match with ==
null == undefined
but undefined != false
typeof null
returns 'object'
, null
is not an object, this is a longstanding bug/quirk that was not fixed in order to maintain compatibility. It's not a true object, and objects are truthy (except for that "wilful violation" document.all
when Javascript is implemented in HTML)NaN
doesn't match anything, with ==
or ===
, not even itself
NaN != NaN
, NaN !== NaN
, NaN != false
, NaN != null
With "strict equality" (===
), there are no such groupings. Only false
===
false
.
This is one of the reasons why many developers and many style guides (e.g. standardjs) prefer ===
and almost never use ==
.
== false
"Truthy" simply means that JavaScript's internal ToBoolean
function returns true
. A quirk of Javascript to be aware of (and another good reason to prefer ===
over ==
): it is possible for a value to be truthy (ToBoolean
returns true
), but also == false
.
You might think if (value && value == false) alert('Huh?')
is a logical impossibility that couldn't happen, but it will, for:
"0"
and '0'
- they're non-empty strings, which are truthy, but Javascript's ==
matches numbers with equivalent strings (e.g. 42 == "42"
). Since 0 == false
, if "0" == 0
, "0" == false
.new Number(0)
and new Boolean(false)
- they're objects, which are truthy, but ==
sees their values, which == false
.0 .toExponential();
- an object with a numerical value equivalent to 0
[]
, [[]]
and [0]
(thanks cloudfeet for the JavaScript Equality Table link)These are just a few values that some people might expect to be falsey, but are actually truthy.
-1
and all non-zero negative numbers
' '
, " "
, "false"
, 'null'
... all non-empty strings, including strings that are just whitespace
Anything from typeof
, which always returns a non-empty string, for example:
typeof null
(returns a string 'object'
due to a longstanding bug/quirk)typeof undefined
(returns a string 'undefined'
)Any object (except that "wilful violation" document.all
in browsers). Remember that null
isn't really an object, despite typeof
suggesting otherwise. Examples:
{}
[]
function(){}
or () => {}
(any function, including empty functions)Error
and any instance of Error
new
(including new Number(0)
and new Boolean(false)
)Any Symbol
true
, 1
, "1"
and [1]
return true
when compared to each other with ==
.
Don't forget about the non-empty string "false"
which evaluates to true
Just to add to @user568458's list of falsy values:
In addition to integer number 0, the decimal number 0.0, 0.00 or any such zeroish number is also a falsy value.
var myNum = 0.0;
if(myNum){
console.log('I am a truthy value');
}
else {
console.log('I am a falsy value');
}
Above code snippet prints I am a falsy value
Similarly hex representation of the number 0 is also a falsy value as shown in below code snippet:
var myNum = 0x0; //hex representation of 0
if(myNum){
console.log('I am a truthy value');
}
else {
console.log('I am a falsy value');
}
Above code snippet again prints I am a falsy value
.
Addition to the topic, as of ES2020 we have a new value which is falsy, it's BigInt zero (0n):
0n == false // true
-0n == false // true
0n === false // false
-0n === false // false
So with this, we now have 7 "falsy" values in total (not including document.all as mentioned by user above since it's part of DOM and not JS).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With