A JavaScript Boolean represents one of two values: true or false. Booleans are named after George Boole and are the backbone of computer logic, conditional testing, and decision-making in your code.
You can use the Boolean() function to find out if an expression (or a
variable) is true or false.
console.log(Boolean(10 > 9)); // true
console.log(10 > 9); // true (the simple way)
In JavaScript, every value has an inherent boolean state. When evaluated in a boolean
context (like an if statement), values are either "truthy" or "falsy".
Everything without a "real" value is false. There are only 6 falsy values in JavaScript:
0 (Zero) and -0"" or '' (Empty string)undefinednullfalseNaN (Not a Number)Everything else is true, including:
100, -5)"Hello", "false"){} and Arrays []Booleans are most commonly used in comparative operations. Comparisons return a boolean result that tells JavaScript which path to take.
let age = 18;
let isAdult = (age >= 18); // Evaluates to true
if (isAdult) {
console.log("You are allowed to enter.");
}
new Boolean(), it is highly discouraged as it can lead to
confusing bugs and slower performance.
A very common mistake for beginners is confusing assignment (=) with
comparison (== or ===). Binary logic only works with comparisons.
let x = 0;
if (x = 10) { }
// This is ALWAYS true because it assigns 10 to x,
// and 10 is a truthy value!
true or false>, <, ===) return boolean results
0, "", null,
undefined, and NaNif