3 Conditionals and Boolean Logic
A progressive guide to JavaScript Boolean values, comparisons, logical operators, conditional branches, and reliable techniques for designing and testing decisions.
Representing Boolean Decisions
Boolean logic gives a program a precise way to represent decisions. A is either true or false, and comparison expressions produce Boolean results. For example, if temperature is 72, the comparison temperature < 80 evaluates to true.
JavaScript also evaluates other values in Boolean contexts. A value behaves like true, while a value behaves like false. The values false, 0, the empty string, null, undefined, and NaN are common values. Arrays and objects, including empty ones, are .
When the program's intent should be explicit, use a comparison or convert a value with Boolean(value) or !!value. Descriptive Boolean names such as isReady, hasPermission, and canSubmit make conditions easier to read.
Takeaway: Begin by identifying the facts a decision depends on, and represent each fact with a clear Boolean expression or descriptive variable.
Comparing Values Precisely
Comparison operators evaluate relationships between values and produce a Boolean result. The most commonly useful comparisons are:
:
===checks that both value and type match.Strict inequality:
!==checks that value or type differs.Greater than and less than:
>and<.Inclusive bounds:
>=and<=.
Prefer and strict inequality in JavaScript. Loose equality, written ==, can convert types automatically; for example, 5 == "5" is true, whereas 5 === "5" is false. Strict comparisons make the intended types visible.
String comparisons follow JavaScript's Unicode ordering, so a simple comparison is not always the same as alphabetical ordering in every language. For user-facing sorting, use locale-aware tools.
Pay close attention to boundary conditions. If a limit of 18 is included, use age >= 18; if it is excluded, use age > 18. Also distinguish assignment, such as score = 10, from comparison, such as score === 10.
Takeaway: Choose an operator that expresses both the relationship and whether the boundary is inclusive.
Combining Conditions
Logical operators combine or reverse conditions. The AND operator && is true only when both conditions are true. The OR operator || is true when at least one condition is true. The NOT operator ! reverses a Boolean condition.
A provides a systematic check:
With
A && B, the result is true only forA = trueandB = true.With
A || B, the result is false only forA = falseandB = false.With
!A, true becomes false and false becomes true.
Use parentheses to show the intended grouping, especially in a condition such as (isAdmin || isOwner) && accountIsActive. This means that an active account is required in addition to being an administrator or owner.
means JavaScript may skip part of a logical expression. In user && user.name, a user prevents JavaScript from attempting to access user.name. Logical operators can also return operands rather than literal Booleans. For example, enteredName || "Guest" selects the entered name when it is and otherwise selects "Guest".
Use ?? instead of || when only null or undefined should trigger a default. The expression suppliedScore ?? 0 preserves meaningful values such as 0.
Takeaway: Translate words carefully: “all” usually requires &&, “at least one” usually requires ||, and “not” requires !.
Building Reliable Branches
A selects a path through a program. An if block runs when its condition is , and an optional else block runs when it is . An else if chain tests several mutually exclusive cases from top to bottom; once one condition is , later branches are skipped.
Order branches from the most specific or highest-priority test to the most general. For example, test age >= 65 before age >= 18; otherwise, every senior also satisfies the broader adult condition and the senior branch can never run.
For a short two-way value choice, the has the form condition ? valueIfTrue : valueIfFalse. For example, age >= 18 ? "adult" : "minor" chooses one category. Use a full if...else structure when branches perform several actions or require multiple statements. Braces are recommended because they make each branch's scope clear.
A reliable design process is:
Identify exactly what decision the program must make.
Name the relevant facts with descriptive variables.
Write each requirement as a separate comparison or Boolean expression.
Combine the requirements according to the wording of the rule.
Order mutually exclusive branches from specific to general.
Test values just below, exactly at, and just above important limits.
For a discount requiring membership and a purchase of at least 50, or alternatively a promotional code, the logic is (isMember && purchaseTotal >= 50) || hasPromoCode. Writing isMember || purchaseTotal >= 50 would incorrectly allow every member to receive the discount, even without the required purchase.
Takeaway: Good conditional logic mirrors the original requirement, makes grouping visible, and is checked with boundary and representative cases.