Conditional statements allow us to make decisions in our code. With conditions, we can execute code only if certain conditions are true.
The if
statement runs code if a condition is true. We can add else
to run different code if the condition is false:
let age = 20;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
The switch
statement is useful for handling multiple conditions based on a single variable: