- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Truthy and Falsy
JavaScript Type System
JavaScript Truthy and Falsy
Every JavaScript value is either truthy or falsy when used as a condition.
There are only eight falsy values; everything else is truthy.
Learning that short list is one of the highest-value things you can memorise.
Example
Example
javascript
if ("hello") {
console.log("a non-empty string is truthy");
}
if (!"") {
console.log("an empty string is falsy");
}Both messages are printed.
The Falsy Values
false, 0, -0, 0n, "", null, undefined and NaN.
That is the complete list.
Everything else is truthy, including "0", [] and {}.
Syntax
Syntax
javascript
if (value) {
// runs when value is truthy
}The value is converted to a boolean automatically.
Surprisingly Truthy
An empty array and an empty object are both truthy.
So is the string "0" and the string "false".
Example
Example
javascript
console.log(Boolean([]));
console.log(Boolean({}));
console.log(Boolean("0"));
console.log(Boolean("false"));All four are true.
Checking an Empty Array
Because [] is truthy, check the length instead.
This catches a great many beginners.
Example
Example
javascript
const items = [];
console.log(items ? "truthy" : "falsy");
console.log(items.length === 0 ? "actually empty" : "has items");The array is truthy even though it is empty.
The Zero Trap
0 is falsy, so a valid count of zero is treated as missing.
This is the bug that nullish coalescing was created to fix.
Example
Example
javascript
const count = 0;
console.log(count ? "has a value" : "looks missing");
console.log(count ?? "really missing");?? keeps the zero; a truthy check does not.
Guarding a Property
A truthy check is fine when any falsy value should be rejected.
Example
Example
javascript
function greet(name) {
if (!name) {
return "Hello stranger";
}
return "Hello " + name;
}
console.log(greet(""));
console.log(greet("Ada"));An empty name falls back, which is what you want here.
Converting Deliberately
Boolean or a double negation makes the intention explicit.
Example
Example
javascript
const value = "hello";
console.log(Boolean(value));
console.log(!!value);Both give true; Boolean is easier to read.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Truthy and Falsy</title>
</head>
<body>
<h1>Truthy and Falsy</h1>
<p id="out"></p>
<script>
const values = [0, "", null, undefined, NaN, false, "0", [], {}, "hello"];
const lines = values.map(function (value) {
const label = Array.isArray(value) ? "[]" :
(value !== null && typeof value === "object") ? "{}" :
JSON.stringify(value) || String(value);
return label + " is " + (value ? "truthy" : "falsy");
});
document.getElementById("out").innerHTML = lines.join("<br>");
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try predicting first:
Guess each answer before running it; the last four catch most people.
Important Points
- There are exactly eight falsy values.
- Everything else is truthy.
[]and{}are truthy even when empty."0"is truthy;0is falsy.- Use
??when zero is a valid value.
Conclusion
Memorising the eight falsy values pays for itself immediately.
The empty array and the zero are the two traps that bite hardest.
Explicit checks are always clearer than relying on truthiness.
