Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use if...else when you need to test a condition, range, or combination of conditions. Use switch when one value should be matched against a set of discrete alternatives. The key difference is how they decide: if tests truthiness, while switch compares values using strict equality. A switch also continues into later case bodies unless you stop it, usually with break.

How do if else statements work in JavaScript?

An if statement evaluates its condition for truthiness. If the condition is truthy, JavaScript runs the statement or block after it. If it is falsy and an else branch is present, JavaScript runs that branch instead. See the MDN if…else reference.

if (isLoggedIn) {
  console.log("Welcome back");
} else {
  console.log("Please sign in");
}

The condition does not have to evaluate to the Boolean primitive true or false. JavaScript converts its value to a Boolean for the branch decision. For example, nonempty strings and nonzero numbers are truthy; false, 0, "", null, and undefined are falsy. A Boolean object created with new Boolean(false) is an object, and objects are truthy—even though its wrapped primitive is false.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use else if for ordered checks

Write else if as two keywords. JavaScript has no elseif keyword. An else if chain checks conditions in order; once one condition is truthy, its branch runs and the remaining branches in that chain are skipped.

const score = 82;

if (score >= 90) {
  console.log("A");
} else if (score >= 70) {
  console.log("Passing");
} else {
  console.log("Keep practicing");
}

Because the first matching branch wins, order matters. Here, a score of 82 fails the first test and passes the second. When conditions overlap, arrange them so the intended condition is tested first.

How does switch work in JavaScript?

A switch evaluates its controlling expression and compares the result with each case value using strict equality (===). When a case matches, execution starts there. If no case matches, a default clause runs when present; without one, execution continues after the switch. See the MDN switch reference.

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");
}

The adjacent Saturday and Sunday cases intentionally share one action: neither has a body of its own, so execution reaches the shared statement. The break after each action exits the switch instead of continuing into the next case body.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does JavaScript switch run the next case?

That behavior is called fall-through. A matching case does not automatically end the switch; execution proceeds through following case bodies until it encounters a control-flow exit such as break, return, or an exception, or reaches the end of the switch. Omitting break by accident can therefore run code for cases that did not match.

const role = "editor";

switch (role) {
  case "editor":
    console.log("Can edit");
  case "admin":
    console.log("Can manage users");
    break;
}

With role set to "editor", both messages print: execution starts at the editor case and falls through to the admin case. Add a break after the editor action if that continuation is not intended. Consecutive empty cases, by contrast, are a common way to route several values to the same body.

Where can default appear?

default is the no-match clause and can appear anywhere in a switch, although placing it last is conventional. If execution enters a default clause positioned before later cases, it can fall through into those later bodies unless a control-flow exit stops it. A switch may contain only one default clause.

What is the difference between if else and switch in JavaScript?

Question if...else switch
What does it test? Whether each condition is truthy; conditions can be arbitrary predicates or combinations. Whether the controlling value strictly equals a case value.
How are alternatives chosen? An else if chain checks in order; the first truthy branch is selected. The first matching case is selected, then execution may continue into later case bodies.
How is the fallback expressed? An optional else handles the path where preceding conditions are falsy. An optional default handles the path where no case matches.
What requires special attention? Condition order when predicates overlap, and the truthiness of non-Boolean values. Strict equality and fall-through when a case lacks a control-flow exit.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should I use switch instead of if else?

  • Choose if...else for ranges, thresholds, multiple variables, or any logic expressed as predicates, such as temperature < 0 or isMember && cartTotal > 50.
  • Choose switch when one expression is compared with a clear set of discrete values, such as a command name, status code, or day of the week.
  • Prefer the form that makes the decision easiest to read. A switch full of range checks or complicated predicates obscures the conditions; an ordered if...else if chain communicates them more directly.

These constructs express different decision shapes; neither should be described as inherently faster based on the language behavior documented here.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.