Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
    Feedback
  1. The else if statement in JavaScript is used to specify a new condition to test if the first condition is false. It allows you to chain multiple conditions together.

    Example

    let time = new Date().getHours();
    let greeting;

    if (time < 10) {
    greeting = "Good morning";
    } else if (time < 20) {
    greeting = "Good day";
    } else {
    greeting = "Good evening";
    }

    console.log(greeting); // Output will depend on the current time
    Copied!

    In this example, the code checks the current hour and assigns a different greeting based on the time of day.

    Important Considerations

    Nesting Conditions

    You can nest multiple else if statements to handle more complex conditions. However, be cautious with deeply nested conditions as they can make your code harder to read and maintain.

    Using Blocks

    Always use block statements {} to group multiple statements within an if, else if, or else clause. This helps avoid errors and makes your code more readable.

    Truthy and Falsy Values

    Feedback
  2. if...else - JavaScript | MDN

    • The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the optional else clause will be executed.
    See more on developer.mozilla.org
  3. JavaScript if, else and else if - GeeksforGeeks

    Apr 15, 2025 · The else if statement allows you to check multiple conditions sequentially. It can be used when you want to test more than one condition and handle each case with a different block of code.

  4. JavaScript if...else Statement (with Examples) - Programiz

      • JavaScript if Statement. The syntax of the if statement is: if (condition) { // the …
      • JavaScript if... else statement. An if statement can have an optional else …
      • JavaScript if... else if statement. The if... else statement is used to execute a …
      • Nested if... else Statement. You can also use an if... else statement inside of an …
      • Body of if... else With Only One Statement. If the body of if... else has only one …
  5. JavaScript if else else if - W3Schools

    Use the else statement to specify a block of code to be executed if the condition is false. If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening": The result of greeting will be: …

  6. JavaScript if else if

    Learn how to use the if...else...if statement to check multiple conditions and execute the corresponding block if a condition is true. See examples of using the if...else...if statement to get the month name, …

  7. JavaScript if-else, else-if Explained with Real Examples

    Jun 23, 2025 · Learn JavaScript if, else, and else if conditional statements with syntax, use cases, and hands-on examples for smarter logic building. Every real …

  8. Conditional branching: if, - The Modern JavaScript Tutorial

    Learn how to use if, else, else if and conditional operator ? to perform different actions based on different conditions in JavaScript. See examples, syntax and …

  9. if statement - "elseif" syntax in JavaScript - Stack Overflow

    Oct 23, 2010 · How can I achieve an elseif in a JavaScript condition? In JavaScript's if-then-else there is technically no elseif branch. But it works if you write it this way: To make it obvious what is really …

  10. JavaScript Conditionals: If, Else and Switch Explaination

    Mar 14, 2026 · Learn JavaScript conditionals with simple examples. Understand if, else, else-if, and switch statements with clear explanations.

  11. People also ask
    Loading
    Unable to load answer