JavaScript・Your syntax toolbox

If it's..., then: the if statement

"If it's the right answer, do ○; if not, do ×"—conditional branching that changes what happens based on the situation. This is why programs look clever.

“If it’s raining, take an umbrella. If it’s sunny, leave it at home.” This kind of judgment we make every day is something you can write into a program with the if statement. Changing what happens based on the situation—this is the real mechanism behind a program acting “clever.”

The basic shape

let weather = "rain";

if (weather === "rain") {
  console.log("Let's bring an umbrella!");
}

Since the contents of weather are “rain,” the console shows “Let’s bring an umbrella!” If the contents were “sunny,” nothing would happen.

PartMeaning
ifThe signal for “if it’s…”
Inside ( )The condition (an expression checked for whether it holds)
Inside { }What to do when the condition holds

”Equal” is three equals signs

When you check “whether they’re equal” inside a condition, you line up ===, three equals signs.

if (weather === "rain") {

When it’s not, use else

You can also write “when it doesn’t hold, do this instead.” That’s else (otherwise).

if (weather === "rain") {
  console.log("Let's bring an umbrella!");
} else {
  console.log("No umbrella needed!");
}

With this, if it’s raining, “Let’s bring an umbrella!”; otherwise, “No umbrella needed!” Because the path splits into two, this mechanism is called “conditional branching.”

You can also compare which number is bigger

Conditions can also include comparisons of numbers.

let score = 85;

if (score >= 80) {
  console.log("Amazing! You passed!");
}
How to writeMeaning
a === ba and b are equal
a !== ba and b are not equal
a > ba is greater than b
a >= ba is greater than or equal to b
a < ba is less than b
a <= ba is less than or equal to b

“Not equal” is written !==. The exclamation mark at the front means “not.”

The true nature of a condition is true and false

By the way, how does an if know whether a condition holds? Actually, the expression you write inside the ( ) turns into one of two answers: true (it holds) or false (it doesn’t). You can check this in the console.

console.log(3 > 1);
console.log(3 > 100);

“true” and “false” show up. What an if does is “if the answer is true, run the { }”—it’s that simple a mechanism.

Let’s see it in action

When you combine it with the “events” you learned before, you can make a gadget like this. The reply changes depending on whether the password matches. There’s one new tool: the text typed into an input field can be pulled out by adding .value to the element you grabbed.

When the button is pressed (event), it checks the typed text (if statement) and writes in the result (textContent). It’s a combo of the ingredients you’ve learned.
JavaScriptRun code when a button is pressedWhen you want to review how “runs when clicked” works, this is the lesson

Summary of this lesson

  1. With if (condition) { what to do }, it runs only when the condition holds
  2. “Equal” is === (three equals signs). One = means “put in,” so it’s a different thing
  3. With else, you can also prepare “the path for when it’s not the case”

Try it

The “if statement” you learned this time won’t be used in Shima’s page. It’s a tool for pages where you want to change what happens based on the situation, like judging a quiz answer or “pass if the score is 80 or more.” Keep it in mind, and use it when you need it.

FAQ

What's the difference between = (one equals) and === (three equals) in an if statement?
= is the symbol for putting a value into a variable, and === is the symbol for checking whether the left and right sides are equal. The one you use in an if condition is ===. If you write = by mistake, what you meant as 'check' becomes 'put in,' creating a bug where the condition is always true.
I heard == (two equals) works too. Should I use === or ==?
== automatically converts the type (whether it's text, a number, and so on) before comparing, so it can give surprising results. While you're a beginner, you'll be fine using only ===, which compares strictly.
Can I leave out the { } (curly braces) after if?
Grammatically you can if there's only one thing to do, but it becomes a source of bugs when you later add a line. Until you're used to it, it's best to always write the { }.