JavaScript uses if and else to choose a branch based on whether a condition is truthy or falsy. Use an else if chain for ordered tests such as ranges, and use switch when one value should be matched against discrete alternatives. A switch match uses strict equality, and execution can continue into later cases unless it exits—usually with break.
How do if else statements work in JavaScript?
An if statement evaluates its condition as a Boolean decision: a truthy result runs the associated statement or block; a falsy result skips it. If an else branch is present, that branch runs when the condition is falsy. See the MDN if…else reference.
As an Amazon Associate I earn from qualifying purchases.
if (condition) {
// Runs when condition is truthy
} else {
// Runs when condition is falsy
}
JavaScript has no elseif keyword. Write else if as two keywords. In a chain, JavaScript checks conditions in order; the first truthy condition selects its branch, and subsequent conditions in that chain are not checked.
Recommended Free Tools
const score = 82;
if (score >= 90) {
console.log("A");
} else if (score >= 70) {
console.log("Passing");
} else {
console.log("Keep practicing");
}
The example prints Passing: the first test is false, the second is true, so the final else is not reached. Because these are range checks, their order matters. Put thresholds in the order that gives each score the intended outcome.
#1 Best Overall
Truthiness is not the same as checking for primitive true
An if condition does not require the primitive Boolean value true. It applies JavaScript truthiness rules. For example, new Boolean(false) creates an object, and objects are truthy—even though that object wraps the value false. If a condition depends on a specific value or type, test it explicitly rather than assuming every truthy value means the primitive true.
What is the difference between if else and switch in JavaScript?
The main distinction is the shape of the decision. An if...else chain can test arbitrary Boolean conditions, including ranges and combinations. A switch evaluates one controlling expression and compares its value with each case value using strict equality (===). It does not coerce values to make a match. See the MDN switch reference.
Rank #2
| Question | if...else |
switch |
|---|---|---|
| What does it test? | Truthiness of each condition, evaluated in order. | One expression against case values using strict equality. |
| What fits naturally? | Ranges, compound tests, and other predicates. | One value matched to discrete alternatives. |
| How is the fallback written? | else runs if no earlier condition succeeds. |
default runs when no case matches. |
| What happens after a selected branch? | Execution continues after the conditional. | Execution continues through following case bodies unless control exits. |
When should I use switch instead of if else?
Choose the construct that makes the decision easiest to read. Use if...else if when branches depend on ranges, multiple variables, or predicates such as temperature < 0. Use switch when one expression—such as a command name or day—is compared with a set of distinct values.
Free tools Windows power users keep installed
One-click scans. No signup required.
const day = "Monday";
switch (day) {
case "Saturday":
case "Sunday":
console.log("Weekend");
break;
case "Monday":
console.log("Start of the week");
break;
default:
console.log("Another weekday");
}
This prints Start of the week. The adjacent weekend cases intentionally share one body: because neither has statements of its own, execution reaches that shared body for either matching value. The break after each action prevents execution from continuing into the next case body.
Do not use switch to disguise range checks
A switch compares values; it does not directly express conditions such as “at least 70” or “between 10 and 20.” Those decisions are clearer as an ordered if...else if chain, where each predicate is visible. Neither construct should be described as inherently faster on the basis of these language semantics; choose for clarity and correct behavior.
Why does JavaScript switch run the next case?
After a matching case is selected, JavaScript begins executing there and keeps running statements in following cases until it encounters a control-flow exit such as break, reaches the end of the switch, or otherwise leaves it. This behavior is called fall-through. It is why omitting a needed break can make more than one case body run.
Rank #4
const choice = "a";
switch (choice) {
case "a":
console.log("A");
// No break: execution falls through
case "b":
console.log("B");
break;
}
With choice set to "a", this prints both A and B. Add a break after the first action if only one case should run. Leave cases grouped without a break only when the shared behavior is intentional and clear.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallDefault is the no-match path, not necessarily the last clause
A switch may contain one default clause. It runs if no case matches. By convention, developers usually put it last, but JavaScript does not require that position. If a switch selects a default placed before later cases, execution can fall through into those later bodies unless it exits. Keeping default last makes the flow easier to inspect.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




