Today, branching. To change what runs depending on a condition, use if.

const budget = 100;
const orangePrice = 80;

if (budget >= orangePrice) {
  console.log('You can buy an orange');
} else {
  console.log('Not enough budget');
}

100 >= 80 holds, so the first message prints.

Right. >= is a comparison operator asking "is the left at least the right?" If the condition holds, the if block runs; otherwise the else block does.

Here is the part worth noticing: the comparison itself is a value.

const canBuy = budget >= orangePrice;

console.log(canBuy);

It printed true. So a condition can have a name too.

It is a value with only two possibilities, true and false. if looks at that value to decide which way to go.

Do I use = to check whether things are equal?

= assigns. To compare for equality use ===, and for inequality !==.

const role = 'admin';

if (role === 'admin') {
  console.log('Showing the admin screen');
} else if (role === 'editor') {
  console.log('Showing the editor screen');
} else {
  console.log('Read-only');
}

else if lets you add more branches.

They are checked from the top, and only the first one that holds runs. So the order you write them in carries meaning.

Conditions can be combined. && asks whether both hold, || whether at least one does.

const age = 20;
const hasTicket = true;

if (age >= 18 && hasTicket) {
  console.log('You may enter');
}

Reading the conditions top to bottom, I can follow which way the program goes.

That instinct is what matters. Say each condition out loud in plain words as you read. Next, let's bundle up a piece of work you use over and over.