JavaScript Tutorial · JavaScript Basics · Lesson 10 of 44
JavaScript Booleans
true, false, comparisons and truthy and falsy values.
Boolean values
A boolean is either true or false. Comparisons produce booleans:
const age = 20;
console.log(age >= 18);
console.log(age === 21);
console.log(10 > 9 && 2 > 3);
Output
true false false
Truthy and falsy
In an if condition, every value counts as true or false. These six values are falsy:
false, 0, "" (empty string), null, undefined, NaN
Everything else is truthy — including "0", "false", [] and {}!
const values = { '0': 0, '""': "", '"0"': "0", '"hello"': "hello", null: null,
undefined: undefined, NaN: NaN, "[]": [], "{}": {} };
for (const [label, v] of Object.entries(values)) {
console.log(label.padEnd(9), Boolean(v) ? "truthy" : "falsy");
}
Output
0 falsy
"" falsy
"0" truthy
"hello" truthy
null falsy
undefined falsy
NaN falsy
[] truthy
{} truthyConvert any value to a boolean with Boolean(value) or !!value.
Test yourself
Which of these values is truthy?
0"""0"null
Show answer
Answer: "0"
Any non-empty string is truthy, even "0".