Skip to content

Exercise 5 of 10 · Conditions

The Fern Check

What you will make

A plant check that compares today's soil moisture with a dry mark, prints the true-or-false answer, and ends on one of two instructions.

The one new idea: a boolean is true or false, and if/else runs one of two blocks depending on it

A program that cannot decide can only recite. Every low-battery warning and every free-delivery message is a comparison producing true or false and one of two outcomes being picked, and boolean is the type TypeScript gives that answer.

Go straight to the code ↓

A type with two values

You have met number and string. The third everyday type is boolean, and it has exactly two possible values: true and false, written without quotes.

You rarely type true or false yourself. They are what comparisons produce:

  • < is less than, and > is greater than
  • <= is less than or equal, and >= is greater than or equal
  • === asks whether two values are the same, and !== whether they differ
TypeScript
const speed: number = 38;
const limit: number = 30;

const tooFast: boolean = speed > limit;

console.log("Too fast:", tooFast);
console.log("Exactly the limit:", speed === limit);
Output
Too fast: true
Exactly the limit: false

speed > limit is a question, and true is its answer. Storing that answer under a name with : boolean gives the decision a name you can read back later.

if/else runs one block

if checks a boolean in round brackets. When it is true, the block in the braces straight after it runs; when it is false, the block after else runs instead. Exactly one of the two, never both.

TypeScript
const speed: number = 38;
const limit: number = 30;
const tooFast: boolean = speed > limit;

if (tooFast) {
  console.log("Slow down.");
} else {
  console.log("Thank you for driving safely.");
}

console.log("Sign checked.");
Output
Slow down.
Sign checked.

The braces decide which lines belong to each side. The indentation inside them is for people reading the code; the compiler only looks at the braces. The last line sits outside both, so it runs whichever way the decision went.

What the compiler catches here

A comparison needs two things of a kind that can be compared. Compare a number with text and it is refused before anything runs:

TypeScript
const speed: number = 38;

console.log(speed > "30");
Output
main.ts(3,13): error TS2365: Operator '>' cannot be applied to types 'number' and 'string'.

One equals sign stores a value; three compare. Use one inside an if and the compiler notices you are trying to store into a const:

TypeScript
const speed: number = 38;
const limit: number = 30;

if (speed = limit) {
  console.log("Right on the limit.");
}
Output
main.ts(4,5): error TS2588: Cannot assign to 'speed' because it is a constant.

Plain JavaScript would only discover that while running. Here nothing runs until the question is written as a question.

Your turn

The editor holds a fern check. moisture is how damp the soil is, and dryBelow is the mark under which the fern needs water. Press Run first: the check says Needs water .... false and tells you to leave the fern, even though 22 is under 30. The answer was typed in instead of worked out.

Replace false on the marked line with a comparison asking whether moisture is less than dryBelow, leave : boolean where it is, and press Run. Once it passes, try changing moisture to 45 and running it again: the same program now tells you to leave the fern for now. The check marks that run as not matching, because it expects today's reading of 22, so put 22 back to see it pass again.

If something goes wrong

If the compiler reports Type 'string' is not assignable to type 'boolean'., the comparison has quotes around it. Quoted, it is a piece of text that happens to contain a less-than sign; unquoted, it is a question with a true-or-false answer.

If the check runs but still says false, read the comparison aloud. moisture > dryBelow asks whether the soil is wetter than the dry mark, which is the opposite question.

If the compiler reports Cannot assign to 'needsWater' because it is a constant., a single = has crept into the if. Its round brackets only need needsWater, which is already true or false.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Putting the comparison in quotes
Quotes turn it into text, and a boolean cannot hold text, so the compiler stops before anything prints with Type 'string' is not assignable to type 'boolean'. A comparison is written bare, the way a sum is.
Pointing the less-than sign the wrong way
moisture > dryBelow asks whether the soil is wetter than the dry mark. It is not, so needsWater comes out false and the check tells you to leave a dry fern alone. Read the line aloud as a question to hear which way round it should be.
Writing one equals sign inside the if
One equals sign stores a value instead of comparing, and needsWater is a const, so the compiler refuses before anything prints: Cannot assign to 'needsWater' because it is a constant.
Typing 30 into the comparison instead of dryBelow
The answer is right today and wrong the day the mark changes. Change dryBelow to 20 and a comparison against a typed 30 carries on using 30, because that number was written into the question instead of looked up.

Want a blank editor instead? Open the TypeScript playground.