{ }codelper

Learn › JavaScript

Control flow · 05

Conditions

Do different things depending on a condition: if · else if · else, plus switch.

A conditional is "if this, then that" in code.
Conditions are checked from the top, and only the first branch that is true runs.
The example below turns a score into a grade and also says whether it is even.

const score = Number(require("fs").readFileSync(0, "utf8"));if (score >= 90) {  console.log("A");} else if (score >= 80) {  console.log("B");} else if (score >= 70) {  console.log("C");} else {  console.log("F");}console.log(score % 2 === 0 ? "even" : "odd");
stdin
75
stdout
C
odd

Key points

  • Conditions are checked from the top, so put the strictest test first.
    If score >= 70 comes first, even 95 becomes C.
  • If every condition is false, the else branch runs.
    else is optional.
  • Logical operators join conditions: and &&, or ||, not !.
    Python uses and, or, not.
    Once the answer is known, the rest is skipped (x != 0 && 10 / x > 1 is safe).
  • To compare one value against many cases, switch is easier to read.
    Languages differ on whether a case "falls through" into the next one.
  • A short either/or can use the ternary operator: cond ? if true : if false (Python: a if cond else b).

How each language writes it

Pythonif cond: / elif cond: / else: — : and indentation instead of braces.
switch since 3.10: match x: + case 1 | 2: + case _:
JavaScriptif (cond) { } else if (cond) { } else { } — in switch, a case without break falls through to the next case.
Values are compared with ===
TypeScriptSame as JavaScript.
Comparing values of different types is caught at compile time
Javaif (cond) { } else if (cond) { } else { } — switch needs break.
Since Java 14, case 1, 2 -> … does not fall through
C#if (cond) { } else if (cond) { } else { } — in switch, every non-empty case must end with break (or return); leaving it out is a compile error
C++if (cond) { } else if (cond) { } else { } — switch works only on integers (characters included); without break it falls through to the next case
CSame as C++.
True and false are any non-zero number and 0
Craykonif (cond) { } else if (cond) { } else { } — switch (x) { case 1, 2: … default: … } runs only the matching case and never falls through

Common mistakes

= instead of ==C · C++ · JavaScript · TypeScript

if (x = 5) {
    printf("five");
}

→ 5 is stored in x, and the condition is always true

Compare with ==.
Java, C#, Python and Craykon report this mistake as an error.

Chaining 1 < x < 10JavaScript · C · C++ · Craykon

let x = 50;
console.log(1 < x < 10);

→ true — (1 < x) becomes true first, and true < 10 is true

Split it: 1 < x && x < 10.
Only Python chains comparisons like math (TypeScript, Java and C# give a compile error).

Forgetting break in a switchC · C++ · Java · JavaScript

switch (n) {
    case 1: puts("one");
    case 2: puts("two");
}

→ When n is 1, both "one" and "two" are printed

End each case with break;.
C# makes this a compile error, and Craykon's switch never falls through in the first place.

How languages differ
In JavaScript and TypeScript, use === for equality: == converts types first, so "1" == 1 and "" == 0 are both true.
Craykon's == is strict like ===, so "1" == 1 is false.

Practice