HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
  • Insights
  • Verify Certificate
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials

HTML5andCSS3.org helps developers learn modern frontend skills with practical tutorials, production-ready snippets, and fast web tools built for everyday coding.

Quick Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Copyright
  • Disclaimer

Top Tools

  • Try It Editor
  • JSON Formatter
  • CSS Minifier
  • HTML Minifier
  • Base64 Encoder / Decoder

Contact

  • business@html5andcss3.org
Newsletter

Learn at your own pace, practice with useful tools, and build websites you're proud of.

© 2011-2026 html5andcss3.org. All rights reserved.

How to Use

JavaScript Tutorial

JavaScript Introduction

What is JavaScriptJavaScript FeaturesJavaScript SyntaxHow to Add JavaScriptJavaScript OutputJavaScript Comments

JavaScript Basics

JavaScript VariablesJavaScript Data TypesJavaScript Operators

JavaScript Statement & Expression

JavaScript StatementsJavaScript ExpressionsJavaScript KeywordsJavaScript IdentifiersJavaScript Case SensitivityJavaScript SemicolonsJavaScript Code Blocks

JavaScript Conditions

JavaScript if StatementJavaScript if else StatementJavaScript else if StatementJavaScript switch Statement

JavaScript Loops

JavaScript for LoopJavaScript while LoopJavaScript do while LoopJavaScript break Statement
  1. Home
  2. /
  3. Tutorials
  4. /
  5. JavaScript Tutorial
  6. /
  7. JavaScript else if Statement

JavaScript Conditions

JavaScript else if Statement

The JavaScript else if statement is used when you need to check multiple conditions.

It allows JavaScript to test one condition after another until a matching condition is found.

Example

Example

javascript

let score = 85;

if (score >= 90) {
  console.log("Grade A");
} else if (score >= 80) {
  console.log("Grade B");
} else {
  console.log("Grade C");
}

In this example, JavaScript checks each condition from top to bottom and runs only the first matching block.

What is an else if Statement?

The else if statement is used when there are more than two possible outcomes.

It is placed between the if block and the else block.

Syntax

Syntax

javascript

if (condition1) {
  // code
} else if (condition2) {
  // code
} else {
  // code
}

JavaScript checks each condition in order.

As soon as one condition is true, the remaining conditions are skipped.

Simple else if Example

Example

javascript

let marks = 72;

if (marks >= 80) {
  console.log("Excellent");
} else if (marks >= 60) {
  console.log("Good");
} else {
  console.log("Needs Improvement");
}

Here, JavaScript checks the conditions one by one until it finds the correct result.

Multiple else if Statements

You can use more than one else if.

Example

javascript

let score = 58;

if (score >= 90) {
  console.log("Grade A");
} else if (score >= 80) {
  console.log("Grade B");
} else if (score >= 70) {
  console.log("Grade C");
} else if (score >= 60) {
  console.log("Grade D");
} else {
  console.log("Grade F");
}

Only one block will run.

else if with Numbers

Example

javascript

let temperature = 18;

if (temperature > 30) {
  console.log("Hot");
} else if (temperature >= 20) {
  console.log("Warm");
} else {
  console.log("Cold");
}

The result depends on the value of temperature.

else if with Strings

Example

javascript

let role = "Editor";

if (role === "Admin") {
  console.log("Full access");
} else if (role === "Editor") {
  console.log("Edit access");
} else {
  console.log("View only");
}

JavaScript compares each string value until a match is found.

Order of Conditions Matters

Always write the most specific or highest-priority conditions first.

Example

javascript

let score = 95;

if (score >= 90) {
  console.log("Grade A");
} else if (score >= 80) {
  console.log("Grade B");
} else {
  console.log("Grade C");
}

If you change the order like this:

Example

javascript

let score = 95;

if (score >= 80) {
  console.log("Grade B");
} else if (score >= 90) {
  console.log("Grade A");
}

The first condition becomes true, so JavaScript never checks the second one.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript else if Statement</title>
</head>
<body>

  <h1>JavaScript else if Statement</h1>

  <p id="result"></p>

  <script>
    let score = 76;
    let grade = "";

    if (score >= 90) {
      grade = "Grade A";
    } else if (score >= 80) {
      grade = "Grade B";
    } else if (score >= 70) {
      grade = "Grade C";
    } else if (score >= 60) {
      grade = "Grade D";
    } else {
      grade = "Grade F";
    }

    document.getElementById("result").innerHTML = grade;
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing the value of score.

For example:

Example

javascript

let score = 92;

or

Example

javascript

let score = 55;

Observe how the output changes for different values.

Important Points

  • The else if statement is used to check multiple conditions.
  • JavaScript checks conditions from top to bottom.
  • Only the first matching block runs.
  • You can use multiple else if blocks in one statement.
  • The else block runs only if none of the conditions are true.
  • Write conditions in the correct order to get the expected result.

Conclusion

The JavaScript else if statement is useful when your program has multiple possible outcomes.

It checks conditions one by one and executes only the first matching block.

Using else if helps you write clear and organized decision-making logic in JavaScript.

PreviousJavaScript if else StatementNextJavaScript switch Statement