Skip to content
Topic 5 of 16
Theory

Conditions

Why it matters

A program must be able to choose: show a "Log in" or "Log out" button, let someone into the game or say "too young", give a discount or not. That's what conditions are for — code that runs only if something is true.

How tasks with functions work

From this topic on, tasks look like this: the editor already contains a "blank" — a function:

function sign(n) {

}

A function is a named piece of code. Data comes into it through a parameter (here n), and it gives back a result with the word return. The checker will call the function with different numbers, e.g. sign(5) and sign(-2), and compare what it returned. Your job is to write the code inside the curly braces. We'll cover functions in detail in topic 7; for now this is enough.

function double(n) {
  return n * 2;
}
console.log(double(4));

Output:

8

if

const temperature = 30;
if (temperature > 25) {
  console.log("It's hot! Take some water.");
}
console.log("Have a nice day");

Output:

It's hot! Take some water.
Have a nice day

The parentheses hold the condition. If it's true, the block in curly braces runs. If it's false, the block is skipped and the program moves on.

else and else if

const score = 72;
if (score >= 90) {
  console.log("Excellent");
} else if (score >= 70) {
  console.log("Good");
} else if (score >= 50) {
  console.log("Satisfactory");
} else {
  console.log("Needs work");
}

Output:

Good

Conditions are checked from top to bottom, and only the first matching branch runs. Even though 72 >= 50 is true too, that check is never reached. So the order matters.

return stops the function

Inside a function, return immediately gives back the result and exits it. That's why you can skip else after return:

function canVote(age) {
  if (age >= 18) return "yes";
  return "no";
}
console.log(canVote(20), canVote(15));

Output:

yes no

If a block has a single command, the curly braces can be omitted, as here. With several commands the braces are required.

Logical operators

Operator Meaning Example
&& AND — both conditions are true age >= 18 && hasTicket
|| OR — at least one is true isAdmin || isOwner
! NOT — flips the value !isLoggedIn
const age = 20;
const hasTicket = true;
console.log(age >= 18 && hasTicket); // true
console.log(age < 12 || age > 65);   // false
console.log(!hasTicket);             // false

You can group with parentheses: (a && b) || c.

The ternary operator ? :

A short "if-else" that returns a value:

const age = 16;
const status = age >= 18 ? "adult" : "teenager";
console.log(status);

Output:

teenager

Read it as: "condition ? value-if-yes : value-if-no". Handy for a simple choice between two options; for complex logic, if is better.

Default values: ?? and ||

const nickname = null;
console.log(nickname ?? "Guest"); // Guest

a ?? b returns b only if a is null or undefined. a || b returns b for any falsy a, including 0 and "". So for numbers ?? is safer: 0 ?? 10 → 0, but 0 || 10 → 10.

switch — choosing by value

const day = "sat";
switch (day) {
  case "sat":
  case "sun":
    console.log("Weekend");
    break;
  case "mon":
    console.log("Monday is hard");
    break;
  default:
    console.log("Weekday");
}

Output:

Weekend

switch compares the value strictly (===) with each case. break is required — without it execution "falls through" into the next case. default runs if nothing matched.

How it works inside

The condition in if doesn't have to be true/false — JavaScript converts it to a boolean using the "falsy" rule: 0, "", null, undefined, NaN are false, everything else is true. So if (name) means "if the name is not empty". And && and || are evaluated lazily: if the answer is already clear from the first part (false && … or true || …), the second part isn't even run.

Common mistakes

  1. = instead of ===. if (x = 5) assigns 5 and is always true. Write if (x === 5).
  2. Wrong else if order. If you check score >= 50 first, "Excellent" will never be given. Start with the strictest conditions.
  3. A double comparison like in maths. 7 <= age <= 17 works incorrectly in JS. You need age >= 7 && age <= 17.
  4. A missing return. A function without return returns undefined — the checker will show that it got undefined.
  5. A missing break in switch. Several branches will run in a row.

Cheat sheet

What How
If if (condition) { … }
Else if / else else if (…) { … } else { … }
AND / OR / NOT &&, ||, !
Ternary cond ? yes : no
Default x ?? "fallback"
Choose by value switch (x) { case …: …; break; default: … }
Return from a function return value;

Sign of a number

Easy+10 XP

Complete the function sign(n): it must return the string

  • "positive" if n is greater than zero;
  • "negative" if it's less than zero;
  • "zero" if n equals zero.
sign(5)   → "positive"
sign(-2)  → "negative"
sign(0)   → "zero"

Connect the connector to solve tasks

Task code runs on your own computer — faster and more reliable than in the browser. This needs the free CodeWithMe connector app. You only connect it once.

Looking for the connector on this computer…

  1. 1
    Download the connector for your system (Windows, macOS or Linux).Download the connectorOn the download page pick the file with your system in its name, e.g. windows-amd64.
  2. 2Run it and choose a folder for your solutions. It will install Python and Node.js there by itself when needed.
  3. 3
    Enter below the 6-digit code shown in the connector window (“Code for CodeWithMe”). If the browser asks about local network access — allow it.

The connector is open source (on GitHub), works only on your computer and runs code only after you enter the pairing code.