How to use booleans and comparisons
Create boolean values
A boolean is exactly true or false. Comparisons produce booleans.
const isMember = true;
const temperature = 24;
const isWarm = temperature >= 20;
console.log(isMember, isWarm);
Use strict equality
Prefer === and !==; they compare without converting operands first.
console.log(5 === 5); // true
console.log("5" === 5); // false
console.log("5" == 5); // true: coercion, usually avoid
Combine conditions
&& means both, || means either, and ! negates a value. Parentheses clarify mixed expressions.
const age = 20;
const hasTicket = true;
const canEnter = age >= 18 && hasTicket;
console.log(canEnter);
console.log(!canEnter);
Understand truthy and falsy
false,0,"",null,undefined, andNaNare falsy- Most other values, including empty arrays and objects, are truthy
- Use explicit comparisons when zero or an empty string is valid data
- Logical operators short-circuit and return operands, not necessarily booleans