JavaScript Booleans
A boolean represents one of two values: `true` or `false`. Booleans are the result of comparisons and logical operations, and drive decision-making in conditionals.
Every JavaScript value has an inherent 'truthiness' โ falsy values include `0`, `''`, `null`, `undefined`, `NaN`, and `false` itself; everything else is truthy.
Falsy values
Only six values are falsy: false, 0, '', null, undefined, and NaN. Everything else โ including '0' the string, and empty objects/arrays โ is truthy.
Boolean() conversion
`Boolean(value)` explicitly converts any value to true or false following truthiness rules, useful for validation checks.
console.log(Boolean(0));
console.log(Boolean(""));
console.log(Boolean("hello"));false
false
trueBoolean() reveals a value's truthiness.
console.log(Boolean([]));
console.log(Boolean({}));true
trueEmpty arrays and objects are truthy, unlike empty strings.
Key points
- Booleans are `true` or `false`.
- Falsy values: false, 0, '', null, undefined, NaN.
- Empty arrays and objects are truthy.
- Boolean(value) converts any value using truthiness rules.
