Conditions - potatoscript/javascript GitHub Wiki
Conditional
πΆ Conditional Statements in JavaScript π§©
In JavaScript, conditional statements allow you to execute different blocks of code based on certain conditions. These conditions are evaluated as true or false. If the condition is true, one block of code will run; if false, another block can run.
This is essential for making decisions in your code and controlling the flow of your program.
π 1. if Statement π‘
The if statement allows you to run a block of code only if a specified condition is true.
Syntax:
if (condition) {
// code to run if condition is true
}
-
Example:
let age = 18; if (age >= 18) { console.log("You are an adult."); }In this example, since
ageis 18 (which is greater than or equal to 18), the message"You are an adult."will be logged to the console.
π 2. else Statement π
The else statement is used to execute a block of code if the condition in the if statement is false.
Syntax:
if (condition) {
// code to run if condition is true
} else {
// code to run if condition is false
}
-
Example:
let age = 15; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are not an adult."); }In this example, since
ageis 15 (which is less than 18), the message"You are not an adult."will be logged to the console.
π 3. else if Statement π
The else if statement allows you to check multiple conditions. If the first condition is false, the program will check the next else if condition, and so on. This is useful when you have more than two possibilities to consider.
Syntax:
if (condition1) {
// code to run if condition1 is true
} else if (condition2) {
// code to run if condition2 is true
} else {
// code to run if all conditions are false
}
-
Example:
let age = 20; 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."); }In this example:
- If
ageis less than 13, it logs"You are a child." - If
ageis between 13 and 19, it logs"You are a teenager." - Otherwise, it logs
"You are an adult."
- If
π 4. switch Statement π
The switch statement allows you to test a variable against multiple possible values. Itβs a cleaner and more efficient way to handle multiple conditions that all check the same variable.
Syntax:
switch (expression) {
case value1:
// code to run if expression equals value1
break;
case value2:
// code to run if expression equals value2
break;
default:
// code to run if expression doesn't match any case
}
-
Example:
let day = "Monday"; switch (day) { case "Monday": console.log("Start of the week."); break; case "Friday": console.log("End of the workweek."); break; case "Saturday": case "Sunday": console.log("It's the weekend!"); break; default: console.log("It's a regular day."); }In this example, the switch statement checks the value of the
dayvariable and logs a message based on the value:- If
dayis"Monday", it logs"Start of the week." - If
dayis"Friday", it logs"End of the workweek." - If
dayis either"Saturday"or"Sunday", it logs"It's the weekend!" - If
dayis anything else, it logs"It's a regular day."
- If
π 5. Ternary Operator π
The ternary operator is a shorthand way of writing an if...else statement. It can be useful for simple conditionals.
Syntax:
condition ? expressionIfTrue : expressionIfFalse;
-
Example:
let age = 16; let result = age >= 18 ? "Adult" : "Minor"; console.log(result); // "Minor"In this example:
- If
ageis greater than or equal to 18, it will assign"Adult"to theresultvariable. - If
ageis less than 18, it will assign"Minor"to theresultvariable.
- If
switch
- to select one of many code blocks to be executed.
switch(document.reg.id.value){
case 'A':
// code block
break;
case 'B':
// code block
break;
default:
// code block
}