Codeloft
0 0 XP

Making decisions with if

7 min 20 XP + 10 for no hints

Programs make choices. if runs a block of code only when a condition is true, and else runs when it's false.

Conditions compare values: === (equal), !== (not equal), >, <, >= and <=. Use three equals signs to compare; a single = stores a value.

Chain choices with else if. JavaScript checks each condition from the top and runs the first one that's true.

Example
let temperature = 31;

if (temperature > 30) {
  console.log("Hot day: drink water");
} else if (temperature > 15) {
  console.log("Nice weather");
} else {
  console.log("Take a jacket");
}
Check the biggest value first. If you test score >= 50 before score >= 90, a 95 would stop at the 50 check.

Your tasks

0 of 3 done
  1. result(40) returns "pass"Expected "pass", got undefined
  2. result(39) returns "fail"Expected "fail", got undefined
  3. grade gives A, B, C and D at the right scoresExpected "AABBCCD", got ""

Quick check

What is the difference between = and === ?

grades.jsPress Run or Ctrl + Enter
undefined undefined