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 age is 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 age is 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 age is less than 13, it logs "You are a child."
    • If age is between 13 and 19, it logs "You are a teenager."
    • Otherwise, it logs "You are an adult."

πŸ“ 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 day variable and logs a message based on the value:

    • If day is "Monday", it logs "Start of the week."
    • If day is "Friday", it logs "End of the workweek."
    • If day is either "Saturday" or "Sunday", it logs "It's the weekend!"
    • If day is anything else, it logs "It's a regular day."

πŸ“ 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 age is greater than or equal to 18, it will assign "Adult" to the result variable.
    • If age is less than 18, it will assign "Minor" to the result variable.

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 
   }