Lesson 6
Lesson 6 of 16
Conditions - if
Lesson Progress: 0%
Code Example
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
if statement checks if something is true.age to see if it is 18 or more.{ } will run.Code Example
let number = 7;
if (number % 2 === 0) {
console.log("Even number");
} else {
console.log("Odd number");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
if statement checks if something is true.else part runs and prints "Odd number".Code Example
let score = 85;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 75) {
console.log("Grade B");
} else if (score >= 50) {
console.log("Grade C");
} else {
console.log("Fail");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
if statement checks the first condition to see if it is true.else if checks another condition.else if until one condition is true.else part runs.Code Example
let username = "admin";
let password = "1234";
if (username === "admin") {
if (password === "1234") {
console.log("Login successful");
} else {
console.log("Wrong password");
}
}Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
if checks if the username is correct.if that checks the password.if because it is inside another if.else part runs and shows a different message.Code Example
let age = 25;
let hasID = true;
if (age >= 18 && hasID) {
console.log("Entry allowed");
}Instructions
▼ ← Click the triangle to hide or reveal instructions.JavaScript Code Editor
Task Incomplete
Editor Input:
Editor Output:
if statement checks if something is true before running the code inside it.&& symbol means "and".if block will happen.