Lesson on If Statements in JavaScript
1. Basic IF Statements
What is an If Statement?
An if statement is a decision-making tool in programming that allows code to run only if a specific condition is true. It makes code responsive to changing inputs or situations.
if (condition) {
// code to execute if condition is true
}
Example of an IF Statement in JavaScript
// Prompt the user for their favorite fruit
let fruit = prompt("What's your favorite fruit?");
// If the fruit is "mango," respond with a specific message
if (fruit === "mango") {
console.log("That sounds super yummy!");
}
2. Basic If-Else and Else-If Statements
What is an If-Else Statement?
An if-else statement expands on the if statement by adding an alternative code block that executes if the initial condition is false. This allows for more complex decision trees in code.
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
Example of an IF-ELSE-IF Statement in JavaScript
// Ask the user for their favorite fruit
let fruit = prompt("What's your favorite fruit?");
// Ensure case-insensitive comparison
if (fruit.toLowerCase() === "mango") {
console.log("That sounds super yummy!");
} else if (fruit.toLowerCase() === "banana") {
console.log("Sounds great!");
} else {
console.log(`Oh, ${fruit} is alright, but mangos are better!`);
}
3. Booleans in If Statements
Using Booleans with If Statements
Booleans allow for true/false checks. In this example, we determine if a user’s favorite fruit is available in stock and display a message accordingly.
// Check if a fruit is available
let isAvailable = true;
if (isAvailable) {
console.log("Your favorite fruit is available!");
} else {
console.log("Sorry, your favorite fruit is out of stock.");
}
4. Using Random Values
Using Random Values with If Statements
Random values create dynamic interactions in a program. In this example, we assign random popularity scores to two fruits and determine which one is more popular.
// Generate random popularity scores for two fruits
let applePopularity = Math.floor(Math.random() * 100) + 1;
let orangePopularity = Math.floor(Math.random() * 100) + 1;
console.log("Apple popularity score:", applePopularity);
console.log("Orange popularity score:", orangePopularity);
if (applePopularity > orangePopularity) {
console.log("Apples are more popular than oranges!");
} else if (applePopularity < orangePopularity) {
console.log("Oranges are more popular than apples!");
} else {
console.log("Both fruits are equally popular!");
}
Summary Table of Conditional Statements
Statement Type | Description | Use Case |
---|---|---|
If Statement | Executes a block of code if a specified condition is true. | Used for simple condition checks, such as verifying if a fruit is "mango". |
If-Else Statement | Executes one block if the condition is true, and another if it is false. | Useful for two-way decision-making, like checking if a fruit is available. |
Else-If Statement | Checks additional conditions if the first condition is false. | Used when comparing multiple values, like determining the more popular fruit. |
Summary
This lesson covers various ways to use conditional statements in JavaScript. Each structure serves a unique purpose in decision-making, from simple checks with if statements to multiple conditions with switch statements.