StreetDocsPlayground
StreetDocs/Make Your Web Page Smart/Making Decisions — if / else
javascript
14 minLearning in Pidgin

Making Decisions — if / else

Teach di computer to choose — 'if Ada get enough money, buy rice; if not, wait.'

What you'll learn

  • Use if and else
  • Compare values with >=, <=, ===, !==, >, <
  • Write code wey dey choose

Before you start

  • numbers-and-strings

Ada get 1000 naira for her hand. Di rice price na 850. She fit buy am? Dis one na a decision — and if / else go handle am for JavaScript.

If / else

let money = 1000;
let price = 850;

if (money >= price) {
  console.log("You fit buy am!");
} else {
  console.log("You need more money.");
}

Di if check di condition for di bracket. If e true, di code inside di first { } run. If e false, di code inside else run.

Comparison operators

  • > greater than · < less than
  • >= greater than or equal · <= less than or equal
  • === equal to · !== not equal to

So Ada don decide — but di computer na do di thinking for am!

Try it yourself

Can Ada buy it?

Open in playground →
javascript
let money = 1000;
let price = 850;

if (money >= price) {
  console.log("You fit buy am!");
} else {
  console.log("You need more money.");
}
Output:
You fit buy am!

Practice exercises

Exercise 1: Decision time

If di money (1000) dey pass or equal di price (850), print 'You fit buy am!'. If not, print 'You need more money.'

Preview
Console

No output yet. Click Run.

Code runs in your browser only — nothing is sent to the server.

Quiz

Small check

Answer di questions below.

1. Which operator dey check if two things dey equal?

2. Wetin 'money >= price' mean?