All falsey values in JavaScript
What are the values in JavaScript that are 'falsey', meaning that they evaluate as false in expressions like if(value)
, value ?
and !value
?
There are some discussions of the purpose of falsey values on Stack Overflow already, but no exhaustive complete answer listing what all the falsey values are.
I couldn't find any complete list on MDN JavaScript Reference, and I was surprised to find that the top results when looking for a complete, authoritative list of falsey values in JavaScript were blog articles, some of which had obvious omissions (for example, NaN
), and none of which had a format like Stack Overflow's where comments or alternative answers could be added to point out quirks, surprises, omissions, mistakes or caveats. So, it seemed to make sense to make one.
javascript
|
show 2 more comments
What are the values in JavaScript that are 'falsey', meaning that they evaluate as false in expressions like if(value)
, value ?
and !value
?
There are some discussions of the purpose of falsey values on Stack Overflow already, but no exhaustive complete answer listing what all the falsey values are.
I couldn't find any complete list on MDN JavaScript Reference, and I was surprised to find that the top results when looking for a complete, authoritative list of falsey values in JavaScript were blog articles, some of which had obvious omissions (for example, NaN
), and none of which had a format like Stack Overflow's where comments or alternative answers could be added to point out quirks, surprises, omissions, mistakes or caveats. So, it seemed to make sense to make one.
javascript
2
Did you check the language spec?
– Pointy
Nov 7 '13 at 15:36
1
@tibos blog.stackoverflow.com/2011/07/…
– user568458
Nov 7 '13 at 15:38
4
dorey.github.io/JavaScript-Equality-Table is a great resource and has anif()
tab for truthiness.
– cloudfeet
May 1 '15 at 13:43
3
Wow, really useful, thanks![] == ""
but!=
?My head hurts...
– user568458
May 1 '15 at 14:30
The ECMA 262 Specification details these comparison rules, specifically sec 11.9.1 through 11.9.6: ecma-international.org/ecma-262/5.1/#sec-11.9.3 It would be swell if you updated your answer to include this reference, since it provides the rules for how true/false determinations are (supposed to be) made.
– Shaun Wilson
Jun 6 '15 at 8:01
|
show 2 more comments
What are the values in JavaScript that are 'falsey', meaning that they evaluate as false in expressions like if(value)
, value ?
and !value
?
There are some discussions of the purpose of falsey values on Stack Overflow already, but no exhaustive complete answer listing what all the falsey values are.
I couldn't find any complete list on MDN JavaScript Reference, and I was surprised to find that the top results when looking for a complete, authoritative list of falsey values in JavaScript were blog articles, some of which had obvious omissions (for example, NaN
), and none of which had a format like Stack Overflow's where comments or alternative answers could be added to point out quirks, surprises, omissions, mistakes or caveats. So, it seemed to make sense to make one.
javascript
What are the values in JavaScript that are 'falsey', meaning that they evaluate as false in expressions like if(value)
, value ?
and !value
?
There are some discussions of the purpose of falsey values on Stack Overflow already, but no exhaustive complete answer listing what all the falsey values are.
I couldn't find any complete list on MDN JavaScript Reference, and I was surprised to find that the top results when looking for a complete, authoritative list of falsey values in JavaScript were blog articles, some of which had obvious omissions (for example, NaN
), and none of which had a format like Stack Overflow's where comments or alternative answers could be added to point out quirks, surprises, omissions, mistakes or caveats. So, it seemed to make sense to make one.
javascript
javascript
edited May 23 '17 at 12:02
Community♦
11
11
asked Nov 7 '13 at 15:34
user568458user568458
16.3k117195
16.3k117195
2
Did you check the language spec?
– Pointy
Nov 7 '13 at 15:36
1
@tibos blog.stackoverflow.com/2011/07/…
– user568458
Nov 7 '13 at 15:38
4
dorey.github.io/JavaScript-Equality-Table is a great resource and has anif()
tab for truthiness.
– cloudfeet
May 1 '15 at 13:43
3
Wow, really useful, thanks![] == ""
but!=
?My head hurts...
– user568458
May 1 '15 at 14:30
The ECMA 262 Specification details these comparison rules, specifically sec 11.9.1 through 11.9.6: ecma-international.org/ecma-262/5.1/#sec-11.9.3 It would be swell if you updated your answer to include this reference, since it provides the rules for how true/false determinations are (supposed to be) made.
– Shaun Wilson
Jun 6 '15 at 8:01
|
show 2 more comments
2
Did you check the language spec?
– Pointy
Nov 7 '13 at 15:36
1
@tibos blog.stackoverflow.com/2011/07/…
– user568458
Nov 7 '13 at 15:38
4
dorey.github.io/JavaScript-Equality-Table is a great resource and has anif()
tab for truthiness.
– cloudfeet
May 1 '15 at 13:43
3
Wow, really useful, thanks![] == ""
but!=
?My head hurts...
– user568458
May 1 '15 at 14:30
The ECMA 262 Specification details these comparison rules, specifically sec 11.9.1 through 11.9.6: ecma-international.org/ecma-262/5.1/#sec-11.9.3 It would be swell if you updated your answer to include this reference, since it provides the rules for how true/false determinations are (supposed to be) made.
– Shaun Wilson
Jun 6 '15 at 8:01
2
2
Did you check the language spec?
– Pointy
Nov 7 '13 at 15:36
Did you check the language spec?
– Pointy
Nov 7 '13 at 15:36
1
1
@tibos blog.stackoverflow.com/2011/07/…
– user568458
Nov 7 '13 at 15:38
@tibos blog.stackoverflow.com/2011/07/…
– user568458
Nov 7 '13 at 15:38
4
4
dorey.github.io/JavaScript-Equality-Table is a great resource and has an
if()
tab for truthiness.– cloudfeet
May 1 '15 at 13:43
dorey.github.io/JavaScript-Equality-Table is a great resource and has an
if()
tab for truthiness.– cloudfeet
May 1 '15 at 13:43
3
3
Wow, really useful, thanks!
[] == ""
but !=
?My head hurts...– user568458
May 1 '15 at 14:30
Wow, really useful, thanks!
[] == ""
but !=
?My head hurts...– user568458
May 1 '15 at 14:30
The ECMA 262 Specification details these comparison rules, specifically sec 11.9.1 through 11.9.6: ecma-international.org/ecma-262/5.1/#sec-11.9.3 It would be swell if you updated your answer to include this reference, since it provides the rules for how true/false determinations are (supposed to be) made.
– Shaun Wilson
Jun 6 '15 at 8:01
The ECMA 262 Specification details these comparison rules, specifically sec 11.9.1 through 11.9.6: ecma-international.org/ecma-262/5.1/#sec-11.9.3 It would be swell if you updated your answer to include this reference, since it provides the rules for how true/false determinations are (supposed to be) made.
– Shaun Wilson
Jun 6 '15 at 8:01
|
show 2 more comments
3 Answers
3
active
oldest
votes
Falsey values in JavaScript
false
0
and other forms of numeric zero like-0
,0.0
and0x0
(credit to RBT for hex form)
""
,''
and``
- strings of length 0null
undefined
NaN
document.all
(in HTML browsers only)
- This is a weird one.
document.all
is a falsey object, withtypeof
asundefined
. 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 becauseif (document.all)
used to be a popular way to detect IE, before conditional comments. See Why is document.all falsy? for details
- This is a weird one.
"Falsey" simply means that JavaScript's internal ToBoolean
function returns false
. ToBoolean
underlies !value
, value ? ... : ...;
and if (value)
. Here's its official specification (2018 working draft) (the only change since the very first ECMAscript specification in 1997 is the addition of ES6's Symbols, which are always truthy):
Comparisons with ==
(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==
- e.g.
false == ""
,'' == 0
and therefore4/2 - 2 == 'some string'.slice(11);
- e.g.
null, undefined
match with==
- e.g.
null == undefined
butundefined != false
- It's also worth mentioning that while
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)
- e.g.
NaN
doesn't match anything, with==
or===
, not even itself
- e.g.
NaN != NaN
,NaN !== NaN
,NaN != false
,NaN != null
- e.g.
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 ==
.
Truthy values that actually == 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"
). Since0 == false
, if"0" == 0
,"0" == false
.
new Number(0)
andnew Boolean(false)
- they're objects, which are truthy, but==
sees their values, which== false
.
0 .toExponential();
- an object with a numerical value equivalent to0
- Any similar constructions that give you a false-equaling value wrapped in a type that is truthy
,
[]
and[0]
(thanks cloudfeet for the JavaScript Equality Table link)
Some more truthy values
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 thatnull
isn't really an object despitetypeof
suggesting otherwise). Including:
{}
function(){}
or() => {}
(any function, including empty functions)
Error
and any instance ofError
- Any regular expression
- Anything created with
new
(includingnew Number(0)
andnew Boolean(false)
)
- Any Symbol
true
, 1
, "1"
and [1]
return true
when compared to each other with ==
.
3
FYI, what!
,if
and?..:
have in common is that they call the internalToBoolean
function on the value. How those values behave in the context of!
,if
, etc. is already implied by their name: They are "falsy" values. I'm a bit afraid that others will read the answer and think "Oh so in this context (!
,if
,?...:
), the value isfalse
, but with!!
, it'strue
", but don't understand the underlying concept. Two other points: 1)v ? true : false
is just a verbose way of!!v
. 2)typeof
always returns a non-empty string, which is truthy.
– Felix Kling
Nov 7 '13 at 15:51
1
I.e. there is no point in looking attypeof null
ortypeof undefined
specifically. You could just say that non-empty strings are truthy.
– Felix Kling
Nov 7 '13 at 15:52
1
I see, but this has nothing to do with the original question ;) Adding too much somewhat related but not relevant information could be rather confusing for readers.
– Felix Kling
Nov 7 '13 at 15:57
5
I have just learned thatdocument.all
is falsy as well.
– Claudiu
Feb 26 '16 at 4:53
3
Regarding loose comparison: because Booleans are converted to numbers,x == false
will callToNumber(x)
which is just very different fromToBoolean(x)
. Might be worth explaining. Also I just noticed that I already commented on this answer ages ago :D
– Felix Kling
Feb 26 '16 at 13:38
|
show 6 more comments
Don't forget about the non-empty string "false"
which evaluates to true
6
…and is therefore not a falsy value, which was asked for?
– Bergi
Oct 4 '17 at 1:23
5
touché. it's a value you might expect to be falsey and isn't, which is still worth being aware of, and I think, merits mention on a comprehensive list
– MrMcPlad
Oct 4 '17 at 21:43
4
Added to the list... but let's not try to turn this into a comprehensive list of all possible truthy values! That'd take a while:-)
– user568458
Dec 7 '17 at 11:15
add a comment |
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
.
5
JavaScript does not have integer numbers.0
,0x0
,0.0
and0.00
are just different literals for the same IEEE-754 64-bit floating point value zero.
– fredoverflow
Nov 24 '17 at 11:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19839952%2fall-falsey-values-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Falsey values in JavaScript
false
0
and other forms of numeric zero like-0
,0.0
and0x0
(credit to RBT for hex form)
""
,''
and``
- strings of length 0null
undefined
NaN
document.all
(in HTML browsers only)
- This is a weird one.
document.all
is a falsey object, withtypeof
asundefined
. 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 becauseif (document.all)
used to be a popular way to detect IE, before conditional comments. See Why is document.all falsy? for details
- This is a weird one.
"Falsey" simply means that JavaScript's internal ToBoolean
function returns false
. ToBoolean
underlies !value
, value ? ... : ...;
and if (value)
. Here's its official specification (2018 working draft) (the only change since the very first ECMAscript specification in 1997 is the addition of ES6's Symbols, which are always truthy):
Comparisons with ==
(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==
- e.g.
false == ""
,'' == 0
and therefore4/2 - 2 == 'some string'.slice(11);
- e.g.
null, undefined
match with==
- e.g.
null == undefined
butundefined != false
- It's also worth mentioning that while
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)
- e.g.
NaN
doesn't match anything, with==
or===
, not even itself
- e.g.
NaN != NaN
,NaN !== NaN
,NaN != false
,NaN != null
- e.g.
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 ==
.
Truthy values that actually == 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"
). Since0 == false
, if"0" == 0
,"0" == false
.
new Number(0)
andnew Boolean(false)
- they're objects, which are truthy, but==
sees their values, which== false
.
0 .toExponential();
- an object with a numerical value equivalent to0
- Any similar constructions that give you a false-equaling value wrapped in a type that is truthy
,
[]
and[0]
(thanks cloudfeet for the JavaScript Equality Table link)
Some more truthy values
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 thatnull
isn't really an object despitetypeof
suggesting otherwise). Including:
{}
function(){}
or() => {}
(any function, including empty functions)
Error
and any instance ofError
- Any regular expression
- Anything created with
new
(includingnew Number(0)
andnew Boolean(false)
)
- Any Symbol
true
, 1
, "1"
and [1]
return true
when compared to each other with ==
.
3
FYI, what!
,if
and?..:
have in common is that they call the internalToBoolean
function on the value. How those values behave in the context of!
,if
, etc. is already implied by their name: They are "falsy" values. I'm a bit afraid that others will read the answer and think "Oh so in this context (!
,if
,?...:
), the value isfalse
, but with!!
, it'strue
", but don't understand the underlying concept. Two other points: 1)v ? true : false
is just a verbose way of!!v
. 2)typeof
always returns a non-empty string, which is truthy.
– Felix Kling
Nov 7 '13 at 15:51
1
I.e. there is no point in looking attypeof null
ortypeof undefined
specifically. You could just say that non-empty strings are truthy.
– Felix Kling
Nov 7 '13 at 15:52
1
I see, but this has nothing to do with the original question ;) Adding too much somewhat related but not relevant information could be rather confusing for readers.
– Felix Kling
Nov 7 '13 at 15:57
5
I have just learned thatdocument.all
is falsy as well.
– Claudiu
Feb 26 '16 at 4:53
3
Regarding loose comparison: because Booleans are converted to numbers,x == false
will callToNumber(x)
which is just very different fromToBoolean(x)
. Might be worth explaining. Also I just noticed that I already commented on this answer ages ago :D
– Felix Kling
Feb 26 '16 at 13:38
|
show 6 more comments
Falsey values in JavaScript
false
0
and other forms of numeric zero like-0
,0.0
and0x0
(credit to RBT for hex form)
""
,''
and``
- strings of length 0null
undefined
NaN
document.all
(in HTML browsers only)
- This is a weird one.
document.all
is a falsey object, withtypeof
asundefined
. 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 becauseif (document.all)
used to be a popular way to detect IE, before conditional comments. See Why is document.all falsy? for details
- This is a weird one.
"Falsey" simply means that JavaScript's internal ToBoolean
function returns false
. ToBoolean
underlies !value
, value ? ... : ...;
and if (value)
. Here's its official specification (2018 working draft) (the only change since the very first ECMAscript specification in 1997 is the addition of ES6's Symbols, which are always truthy):
Comparisons with ==
(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==
- e.g.
false == ""
,'' == 0
and therefore4/2 - 2 == 'some string'.slice(11);
- e.g.
null, undefined
match with==
- e.g.
null == undefined
butundefined != false
- It's also worth mentioning that while
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)
- e.g.
NaN
doesn't match anything, with==
or===
, not even itself
- e.g.
NaN != NaN
,NaN !== NaN
,NaN != false
,NaN != null
- e.g.
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 ==
.
Truthy values that actually == 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"
). Since0 == false
, if"0" == 0
,"0" == false
.
new Number(0)
andnew Boolean(false)
- they're objects, which are truthy, but==
sees their values, which== false
.
0 .toExponential();
- an object with a numerical value equivalent to0
- Any similar constructions that give you a false-equaling value wrapped in a type that is truthy
,
[]
and[0]
(thanks cloudfeet for the JavaScript Equality Table link)
Some more truthy values
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 thatnull
isn't really an object despitetypeof
suggesting otherwise). Including:
{}
function(){}
or() => {}
(any function, including empty functions)
Error
and any instance ofError
- Any regular expression
- Anything created with
new
(includingnew Number(0)
andnew Boolean(false)
)
- Any Symbol
true
, 1
, "1"
and [1]
return true
when compared to each other with ==
.
3
FYI, what!
,if
and?..:
have in common is that they call the internalToBoolean
function on the value. How those values behave in the context of!
,if
, etc. is already implied by their name: They are "falsy" values. I'm a bit afraid that others will read the answer and think "Oh so in this context (!
,if
,?...:
), the value isfalse
, but with!!
, it'strue
", but don't understand the underlying concept. Two other points: 1)v ? true : false
is just a verbose way of!!v
. 2)typeof
always returns a non-empty string, which is truthy.
– Felix Kling
Nov 7 '13 at 15:51
1
I.e. there is no point in looking attypeof null
ortypeof undefined
specifically. You could just say that non-empty strings are truthy.
– Felix Kling
Nov 7 '13 at 15:52
1
I see, but this has nothing to do with the original question ;) Adding too much somewhat related but not relevant information could be rather confusing for readers.
– Felix Kling
Nov 7 '13 at 15:57
5
I have just learned thatdocument.all
is falsy as well.
– Claudiu
Feb 26 '16 at 4:53
3
Regarding loose comparison: because Booleans are converted to numbers,x == false
will callToNumber(x)
which is just very different fromToBoolean(x)
. Might be worth explaining. Also I just noticed that I already commented on this answer ages ago :D
– Felix Kling
Feb 26 '16 at 13:38
|
show 6 more comments
Falsey values in JavaScript
false
0
and other forms of numeric zero like-0
,0.0
and0x0
(credit to RBT for hex form)
""
,''
and``
- strings of length 0null
undefined
NaN
document.all
(in HTML browsers only)
- This is a weird one.
document.all
is a falsey object, withtypeof
asundefined
. 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 becauseif (document.all)
used to be a popular way to detect IE, before conditional comments. See Why is document.all falsy? for details
- This is a weird one.
"Falsey" simply means that JavaScript's internal ToBoolean
function returns false
. ToBoolean
underlies !value
, value ? ... : ...;
and if (value)
. Here's its official specification (2018 working draft) (the only change since the very first ECMAscript specification in 1997 is the addition of ES6's Symbols, which are always truthy):
Comparisons with ==
(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==
- e.g.
false == ""
,'' == 0
and therefore4/2 - 2 == 'some string'.slice(11);
- e.g.
null, undefined
match with==
- e.g.
null == undefined
butundefined != false
- It's also worth mentioning that while
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)
- e.g.
NaN
doesn't match anything, with==
or===
, not even itself
- e.g.
NaN != NaN
,NaN !== NaN
,NaN != false
,NaN != null
- e.g.
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 ==
.
Truthy values that actually == 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"
). Since0 == false
, if"0" == 0
,"0" == false
.
new Number(0)
andnew Boolean(false)
- they're objects, which are truthy, but==
sees their values, which== false
.
0 .toExponential();
- an object with a numerical value equivalent to0
- Any similar constructions that give you a false-equaling value wrapped in a type that is truthy
,
[]
and[0]
(thanks cloudfeet for the JavaScript Equality Table link)
Some more truthy values
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 thatnull
isn't really an object despitetypeof
suggesting otherwise). Including:
{}
function(){}
or() => {}
(any function, including empty functions)
Error
and any instance ofError
- Any regular expression
- Anything created with
new
(includingnew Number(0)
andnew Boolean(false)
)
- Any Symbol
true
, 1
, "1"
and [1]
return true
when compared to each other with ==
.
Falsey values in JavaScript
false
0
and other forms of numeric zero like-0
,0.0
and0x0
(credit to RBT for hex form)
""
,''
and``
- strings of length 0null
undefined
NaN
document.all
(in HTML browsers only)
- This is a weird one.
document.all
is a falsey object, withtypeof
asundefined
. 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 becauseif (document.all)
used to be a popular way to detect IE, before conditional comments. See Why is document.all falsy? for details
- This is a weird one.
"Falsey" simply means that JavaScript's internal ToBoolean
function returns false
. ToBoolean
underlies !value
, value ? ... : ...;
and if (value)
. Here's its official specification (2018 working draft) (the only change since the very first ECMAscript specification in 1997 is the addition of ES6's Symbols, which are always truthy):
Comparisons with ==
(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==
- e.g.
false == ""
,'' == 0
and therefore4/2 - 2 == 'some string'.slice(11);
- e.g.
null, undefined
match with==
- e.g.
null == undefined
butundefined != false
- It's also worth mentioning that while
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)
- e.g.
NaN
doesn't match anything, with==
or===
, not even itself
- e.g.
NaN != NaN
,NaN !== NaN
,NaN != false
,NaN != null
- e.g.
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 ==
.
Truthy values that actually == 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"
). Since0 == false
, if"0" == 0
,"0" == false
.
new Number(0)
andnew Boolean(false)
- they're objects, which are truthy, but==
sees their values, which== false
.
0 .toExponential();
- an object with a numerical value equivalent to0
- Any similar constructions that give you a false-equaling value wrapped in a type that is truthy
,
[]
and[0]
(thanks cloudfeet for the JavaScript Equality Table link)
Some more truthy values
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 thatnull
isn't really an object despitetypeof
suggesting otherwise). Including:
{}
function(){}
or() => {}
(any function, including empty functions)
Error
and any instance ofError
- Any regular expression
- Anything created with
new
(includingnew Number(0)
andnew Boolean(false)
)
- Any Symbol
true
, 1
, "1"
and [1]
return true
when compared to each other with ==
.
edited Aug 6 '18 at 13:30
answered Nov 7 '13 at 15:34
user568458user568458
16.3k117195
16.3k117195
3
FYI, what!
,if
and?..:
have in common is that they call the internalToBoolean
function on the value. How those values behave in the context of!
,if
, etc. is already implied by their name: They are "falsy" values. I'm a bit afraid that others will read the answer and think "Oh so in this context (!
,if
,?...:
), the value isfalse
, but with!!
, it'strue
", but don't understand the underlying concept. Two other points: 1)v ? true : false
is just a verbose way of!!v
. 2)typeof
always returns a non-empty string, which is truthy.
– Felix Kling
Nov 7 '13 at 15:51
1
I.e. there is no point in looking attypeof null
ortypeof undefined
specifically. You could just say that non-empty strings are truthy.
– Felix Kling
Nov 7 '13 at 15:52
1
I see, but this has nothing to do with the original question ;) Adding too much somewhat related but not relevant information could be rather confusing for readers.
– Felix Kling
Nov 7 '13 at 15:57
5
I have just learned thatdocument.all
is falsy as well.
– Claudiu
Feb 26 '16 at 4:53
3
Regarding loose comparison: because Booleans are converted to numbers,x == false
will callToNumber(x)
which is just very different fromToBoolean(x)
. Might be worth explaining. Also I just noticed that I already commented on this answer ages ago :D
– Felix Kling
Feb 26 '16 at 13:38
|
show 6 more comments
3
FYI, what!
,if
and?..:
have in common is that they call the internalToBoolean
function on the value. How those values behave in the context of!
,if
, etc. is already implied by their name: They are "falsy" values. I'm a bit afraid that others will read the answer and think "Oh so in this context (!
,if
,?...:
), the value isfalse
, but with!!
, it'strue
", but don't understand the underlying concept. Two other points: 1)v ? true : false
is just a verbose way of!!v
. 2)typeof
always returns a non-empty string, which is truthy.
– Felix Kling
Nov 7 '13 at 15:51
1
I.e. there is no point in looking attypeof null
ortypeof undefined
specifically. You could just say that non-empty strings are truthy.
– Felix Kling
Nov 7 '13 at 15:52
1
I see, but this has nothing to do with the original question ;) Adding too much somewhat related but not relevant information could be rather confusing for readers.
– Felix Kling
Nov 7 '13 at 15:57
5
I have just learned thatdocument.all
is falsy as well.
– Claudiu
Feb 26 '16 at 4:53
3
Regarding loose comparison: because Booleans are converted to numbers,x == false
will callToNumber(x)
which is just very different fromToBoolean(x)
. Might be worth explaining. Also I just noticed that I already commented on this answer ages ago :D
– Felix Kling
Feb 26 '16 at 13:38
3
3
FYI, what
!
, if
and ?..:
have in common is that they call the internal ToBoolean
function on the value. How those values behave in the context of !
, if
, etc. is already implied by their name: They are "falsy" values. I'm a bit afraid that others will read the answer and think "Oh so in this context (!
, if
, ?...:
), the value is false
, but with !!
, it's true
", but don't understand the underlying concept. Two other points: 1) v ? true : false
is just a verbose way of !!v
. 2) typeof
always returns a non-empty string, which is truthy.– Felix Kling
Nov 7 '13 at 15:51
FYI, what
!
, if
and ?..:
have in common is that they call the internal ToBoolean
function on the value. How those values behave in the context of !
, if
, etc. is already implied by their name: They are "falsy" values. I'm a bit afraid that others will read the answer and think "Oh so in this context (!
, if
, ?...:
), the value is false
, but with !!
, it's true
", but don't understand the underlying concept. Two other points: 1) v ? true : false
is just a verbose way of !!v
. 2) typeof
always returns a non-empty string, which is truthy.– Felix Kling
Nov 7 '13 at 15:51
1
1
I.e. there is no point in looking at
typeof null
or typeof undefined
specifically. You could just say that non-empty strings are truthy.– Felix Kling
Nov 7 '13 at 15:52
I.e. there is no point in looking at
typeof null
or typeof undefined
specifically. You could just say that non-empty strings are truthy.– Felix Kling
Nov 7 '13 at 15:52
1
1
I see, but this has nothing to do with the original question ;) Adding too much somewhat related but not relevant information could be rather confusing for readers.
– Felix Kling
Nov 7 '13 at 15:57
I see, but this has nothing to do with the original question ;) Adding too much somewhat related but not relevant information could be rather confusing for readers.
– Felix Kling
Nov 7 '13 at 15:57
5
5
I have just learned that
document.all
is falsy as well.– Claudiu
Feb 26 '16 at 4:53
I have just learned that
document.all
is falsy as well.– Claudiu
Feb 26 '16 at 4:53
3
3
Regarding loose comparison: because Booleans are converted to numbers,
x == false
will call ToNumber(x)
which is just very different from ToBoolean(x)
. Might be worth explaining. Also I just noticed that I already commented on this answer ages ago :D– Felix Kling
Feb 26 '16 at 13:38
Regarding loose comparison: because Booleans are converted to numbers,
x == false
will call ToNumber(x)
which is just very different from ToBoolean(x)
. Might be worth explaining. Also I just noticed that I already commented on this answer ages ago :D– Felix Kling
Feb 26 '16 at 13:38
|
show 6 more comments
Don't forget about the non-empty string "false"
which evaluates to true
6
…and is therefore not a falsy value, which was asked for?
– Bergi
Oct 4 '17 at 1:23
5
touché. it's a value you might expect to be falsey and isn't, which is still worth being aware of, and I think, merits mention on a comprehensive list
– MrMcPlad
Oct 4 '17 at 21:43
4
Added to the list... but let's not try to turn this into a comprehensive list of all possible truthy values! That'd take a while:-)
– user568458
Dec 7 '17 at 11:15
add a comment |
Don't forget about the non-empty string "false"
which evaluates to true
6
…and is therefore not a falsy value, which was asked for?
– Bergi
Oct 4 '17 at 1:23
5
touché. it's a value you might expect to be falsey and isn't, which is still worth being aware of, and I think, merits mention on a comprehensive list
– MrMcPlad
Oct 4 '17 at 21:43
4
Added to the list... but let's not try to turn this into a comprehensive list of all possible truthy values! That'd take a while:-)
– user568458
Dec 7 '17 at 11:15
add a comment |
Don't forget about the non-empty string "false"
which evaluates to true
Don't forget about the non-empty string "false"
which evaluates to true
answered Oct 4 '17 at 1:16
MrMcPladMrMcPlad
12925
12925
6
…and is therefore not a falsy value, which was asked for?
– Bergi
Oct 4 '17 at 1:23
5
touché. it's a value you might expect to be falsey and isn't, which is still worth being aware of, and I think, merits mention on a comprehensive list
– MrMcPlad
Oct 4 '17 at 21:43
4
Added to the list... but let's not try to turn this into a comprehensive list of all possible truthy values! That'd take a while:-)
– user568458
Dec 7 '17 at 11:15
add a comment |
6
…and is therefore not a falsy value, which was asked for?
– Bergi
Oct 4 '17 at 1:23
5
touché. it's a value you might expect to be falsey and isn't, which is still worth being aware of, and I think, merits mention on a comprehensive list
– MrMcPlad
Oct 4 '17 at 21:43
4
Added to the list... but let's not try to turn this into a comprehensive list of all possible truthy values! That'd take a while:-)
– user568458
Dec 7 '17 at 11:15
6
6
…and is therefore not a falsy value, which was asked for?
– Bergi
Oct 4 '17 at 1:23
…and is therefore not a falsy value, which was asked for?
– Bergi
Oct 4 '17 at 1:23
5
5
touché. it's a value you might expect to be falsey and isn't, which is still worth being aware of, and I think, merits mention on a comprehensive list
– MrMcPlad
Oct 4 '17 at 21:43
touché. it's a value you might expect to be falsey and isn't, which is still worth being aware of, and I think, merits mention on a comprehensive list
– MrMcPlad
Oct 4 '17 at 21:43
4
4
Added to the list... but let's not try to turn this into a comprehensive list of all possible truthy values! That'd take a while
:-)
– user568458
Dec 7 '17 at 11:15
Added to the list... but let's not try to turn this into a comprehensive list of all possible truthy values! That'd take a while
:-)
– user568458
Dec 7 '17 at 11:15
add a comment |
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
.
5
JavaScript does not have integer numbers.0
,0x0
,0.0
and0.00
are just different literals for the same IEEE-754 64-bit floating point value zero.
– fredoverflow
Nov 24 '17 at 11:08
add a comment |
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
.
5
JavaScript does not have integer numbers.0
,0x0
,0.0
and0.00
are just different literals for the same IEEE-754 64-bit floating point value zero.
– fredoverflow
Nov 24 '17 at 11:08
add a comment |
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
.
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
.
edited Jan 24 '18 at 11:36
answered Sep 20 '17 at 0:10
RBTRBT
9,075672101
9,075672101
5
JavaScript does not have integer numbers.0
,0x0
,0.0
and0.00
are just different literals for the same IEEE-754 64-bit floating point value zero.
– fredoverflow
Nov 24 '17 at 11:08
add a comment |
5
JavaScript does not have integer numbers.0
,0x0
,0.0
and0.00
are just different literals for the same IEEE-754 64-bit floating point value zero.
– fredoverflow
Nov 24 '17 at 11:08
5
5
JavaScript does not have integer numbers.
0
, 0x0
, 0.0
and 0.00
are just different literals for the same IEEE-754 64-bit floating point value zero.– fredoverflow
Nov 24 '17 at 11:08
JavaScript does not have integer numbers.
0
, 0x0
, 0.0
and 0.00
are just different literals for the same IEEE-754 64-bit floating point value zero.– fredoverflow
Nov 24 '17 at 11:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19839952%2fall-falsey-values-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Did you check the language spec?
– Pointy
Nov 7 '13 at 15:36
1
@tibos blog.stackoverflow.com/2011/07/…
– user568458
Nov 7 '13 at 15:38
4
dorey.github.io/JavaScript-Equality-Table is a great resource and has an
if()
tab for truthiness.– cloudfeet
May 1 '15 at 13:43
3
Wow, really useful, thanks!
[] == ""
but!=
?My head hurts...– user568458
May 1 '15 at 14:30
The ECMA 262 Specification details these comparison rules, specifically sec 11.9.1 through 11.9.6: ecma-international.org/ecma-262/5.1/#sec-11.9.3 It would be swell if you updated your answer to include this reference, since it provides the rules for how true/false determinations are (supposed to be) made.
– Shaun Wilson
Jun 6 '15 at 8:01