Learn how JavaScript evaluates truthy and falsy expressions and why strict equality (===) prevents silent type coercion bugs in production.
Learn how variables act as labeled containers for data using const, let, and var to manage lexical scope and reassignment safely in modern applications.
A boolean represents a logical state with only two possible values: true or false. Booleans are the engine behind conditional branching in software.
When non-boolean values (like strings, numbers, or objects) are evaluated in conditional contexts (like if statements), JavaScript implicitly converts them into either truthy or falsy values. Understanding these rules prevents unexpected control flow bugs.
In JavaScript, exactly 6 values evaluate to false in a boolean context. Memorizing these 6 makes understanding conditional checks effortless:
false (The literal boolean)0 (including -0 and 0n)"" (Empty string)null (Intentional empty reference)undefined (Uninitialized variable)NaN (Not-a-Number)EVERY OTHER VALUE IN JAVASCRIPT IS TRUTHY! This includes empty arrays [], empty objects {}, and the string "false".
const userSearch = "";
if (userSearch) {
console.log("Searching...");
} else {
console.log("Please enter a search query."); // Runs because "" is falsy!
}
Loose equality (==) performs implicit type coercion before comparing values. Strict equality (===) compares both value AND data type without converting types:
console.log(0 == "0"); // true (Loose coercion! Dangerous!)
console.log(0 === "0"); // false (Strict comparison! Safe!)
console.log(null == undefined); // true
console.log(null === undefined); // false
[] == false is true!). Always default to strict equality (===).[] and {} are truthy objects! Checking if (myArray) on an empty array will evaluate to true. To check if an array is empty, check its length: myArray.length > 0.Always use === for comparisons. Next, let's learn how to branch application execution using if, else, and ternary operators!