Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Control Flow in JavaScript: If, Else, and Switch Explained

Understand JavaScript branching: use if/else for truthy conditions and ranges, switch for discrete values, and break to prevent unintended fall-through.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

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

Default 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.