18. The Boolean Type
The Boolean type represents logical entities and can only have two values: true or false. They are fundamental for making decisions in code (control flow).
Defining Booleans
javascript const isLoggedIn = true; let isAdmin = false;
Booleans from Comparison
Booleans are typically generated by comparison operators (covered in detail soon).
javascript let a = 10; let b = 5;
let isGreater = (a > b); // Is 10 greater than 5? Yes. console.log(isGreater); // Output: true
let isEqual = (a === b); // Is 10 strictly equal to 5? No. console.log(isEqual); // Output: false
Boolean Context (Truthiness)
In decision structures (like if statements), JS automatically converts values to a boolean state. This is called truthiness.
Falsy Values (Convert to false):
false0(the number zero)''(an empty string)nullundefinedNaN
Truthy Values (Convert to true):
- Everything else (e.g.,
1,'hello',[],{})