12.2 If Statements and If-Else
If Statements and If-Else (20 mins)
if (condition) { // code to run if the condition is true }
let age = 18; if (age >= 18) { console.log("You are eligible to vote."); }if (condition) { // code to run if condition is true } else { // code to run if condition is false }
let age = 16; if (age >= 18) { console.log("You are eligible to vote."); } else { console.log("You are not eligible to vote."); }if (condition1) { // code to run if condition1 is true } else if (condition2) { // code to run if condition2 is true } else { // code to run if neither condition1 nor condition2 is true }
let age = 17; if (age < 13) { console.log("You are a child."); } else if (age >= 13 && age <= 19) { console.log("You are a teenager."); } else { console.log("You are an adult."); }if (0) { console.log("This won't run because 0 is falsy."); } if ("Hello") { console.log("This will run because non-empty strings are truthy."); }
Student Activity (20 mins):
Last updated