Control flow: decisions and loops · Part 4 of 36

Decisions with if, else and booleans

Basic 13 min

Comparison and logical operators, if/else chains, truthy and falsy values — in JavaScript and Python.

What you will learn

  • You can write if / else if / else (elif) chains in both languages.
  • You can combine conditions with and/or/not.
  • You can predict which values count as true or false.

Before this: Variables and data types

Booleans: true or false

A condition is an expression that is either true or false — a boolean. You get booleans from comparisons:

MeaningJavaScriptPython
equala === ba == b
not equala !== ba != b
less / greater<, >, <=, >=<, >, <=, >=
and / not&&, !and, not

"Or" is written as two vertical bars in JavaScript and as or in Python.

A railway switch sending a small train to the left or the right track

if, else if, else

// JavaScript
const temp = 4;
if (temp < 0) {
  console.log("Frost: cover the seedlings");
} else if (temp < 5) {
  console.log("Cold: keep them inside tonight");
} else {
  console.log("Fine: leave them out");
}
# Python
temp = 4
if temp < 0:
    print("Frost: cover the seedlings")
elif temp < 5:
    print("Cold: keep them inside tonight")
else:
    print("Fine: leave them out")

Only the first branch whose condition is true runs; the others are skipped. Order matters: if you checked temp < 5 first, the frost message could never appear.

JavaScript groups the lines of a branch with curly braces { }. Python uses indentation — the spaces at the start of the line are part of the syntax. Use four spaces per level and never mix tabs and spaces.

Combining conditions

if is_weekend and not raining:
    print("Garden day!")

and is true only if both sides are true; or if at least one is. Both languages short-circuit: in a and b, if a is false, b is not even evaluated. That lets you write safe checks such as user && user.name (JavaScript) or user is not None and user.name (Python).

Python also lets you chain comparisons: 0 <= x < 10 means 0 <= x and x < 10. In JavaScript 0 <= x < 10 is a trap: it compares (0 <= x) — a boolean — with 10, and is always true.

Truthy and falsy

Conditions do not have to be booleans; other values are converted.

  • Falsy in JavaScript: false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy — including "0", [] and {}.
  • Falsy in Python: False, None, 0, 0.0, "", and empty collections such as [], {}, set().

Note the difference: an empty list is falsy in Python but truthy in JavaScript. To test for an empty array in JavaScript, use list.length === 0.

Choosing between two values

For short decisions there is a compact form:

const label = score >= 70 ? "passed" : "try again";
label = "passed" if score >= 70 else "try again"

For many cases on one value, Python 3.10 added match/case; JavaScript has the older switch statement (remember break at the end of each case).

Try it: JavaScript sandbox

Write and run small JavaScript programs; the checker tells you when an exercise is solved.

Turn on JavaScript to use this lab. Worked example:

Exercise 4: complete the function parity(n) so that it returns "even" when n is divisible by 2 and "odd" otherwise. Hint: n % 2 === 0 checks the remainder of the division by 2; the checker tries 4, 7 and 0.

Key points

  • Conditions are booleans, usually from comparisons such as ===/==, <, >=.
  • if / else if / else (elif in Python) runs only the first matching branch.
  • Combine conditions with and/or/not (&&, two bars, ! in JavaScript); both languages short-circuit.
  • Non-boolean values are truthy or falsy; empty lists are falsy only in Python.
  • Ternary: c ? a : b in JavaScript, a if c else b in Python.

Flashcards

Question: How does Python mark the lines that belong to an if-branch?
Answer: By indentation (usually four spaces).
Question: Is an empty array truthy in JavaScript?
Answer: Yes — [] is truthy in JavaScript, while an empty list is falsy in Python.
Question: What does short-circuit evaluation mean?
Answer: The right side of and/or is only evaluated if the left side does not already decide the result.
Question: Write "passed or try again" as a Python conditional expression.
Answer: "passed" if score >= 70 else "try again"

Check yourself

Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.

  1. temp = 4. Which message does the example print?

    • Cold: keep them inside tonight
    • Frost: cover the seedlings
    • Fine: leave them out
    • Nothing
    Show the answer

    Cold: keep them inside tonight

    4 is not below 0, but it is below 5, so the elif branch runs.

    Source: The Python Tutorial, 4.1 if Statements

  2. Which values are falsy in JavaScript?

    • 0
    • ""
    • []
    • null
    Show the answer

    0; ""; null

    0, the empty string and null are falsy; an empty array is an object and truthy.

    Source: MDN, Falsy

  3. What does 0 <= 15 < 10 evaluate to in JavaScript?

    • true
    • false
    • an error
    • undefined
    Show the answer

    true

    (0 <= 15) is true, and true < 10 converts true to 1, so the result is true — a classic trap.

    Source: MDN, Relational operators

  4. Python’s keyword for "else if" is ___.

    Show the answer

    elif

    Python writes else-if as elif.

    Source: The Python Tutorial, 4.1

Sources

Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.