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 if Statement

JavaScript Conditions

JavaScript if Statement

The JavaScript if statement is used to run a block of code only when a condition is true.

It helps JavaScript make decisions.

Example

Example

javascript

let age = 20;

if (age >= 18) {
  console.log("You are eligible.");
}

In this example, the code inside the if block runs only when age is greater than or equal to 18.

What is an if Statement?

An if statement checks a condition.

If the condition is true, JavaScript runs the code inside the curly braces {}.

If the condition is false, JavaScript skips that code.

Syntax

Syntax

javascript

if (condition) {
  // code runs when condition is true
}

The condition is written inside parentheses ().

The code block is written inside curly braces {}.

Simple if Statement Example

Example

javascript

let marks = 75;

if (marks >= 40) {
  console.log("You passed the exam.");
}

Here, the condition is:

Condition

javascript

marks >= 40

Since marks is greater than 40, the message will run.

if Statement with Numbers

You can use an if statement to compare numbers.

Example

javascript

let price = 1200;

if (price > 1000) {
  console.log("This product is expensive.");
}

Here, the message runs only if price is greater than 1000.

if Statement with Strings

You can also compare string values.

Example

javascript

let userName = "John";

if (userName === "John") {
  console.log("Hello John!");
}

Here, the condition checks whether userName is exactly equal to "John".

if Statement with Boolean Values

Boolean values are commonly used with if statements.

Example

javascript

let isLoggedIn = true;

if (isLoggedIn) {
  console.log("Welcome back!");
}

This is the same as writing:

Example

javascript

if (isLoggedIn === true) {
  console.log("Welcome back!");
}

The shorter version is cleaner and commonly used.

Using Comparison Operators with if

Comparison operators are often used inside if conditions.

OperatorMeaning
<code>></code>Greater than
<code><</code>Less than
<code>>=</code>Greater than or equal to
<code><=</code>Less than or equal to
<code>==</code>Equal value
<code>===</code>Equal value and equal type
<code>!=</code>Not equal
<code>!==</code>Not equal value or not equal type

Example

javascript

let score = 90;

if (score >= 80) {
  console.log("Excellent score.");
}

Using Logical Operators with if

Logical operators are used when you want to check more than one condition.

Example

javascript

let age = 22;
let hasId = true;

if (age >= 18 && hasId) {
  console.log("You can enter.");
}

Here, both conditions must be true:

ConditionMeaning
<code>age >= 18</code>Age must be 18 or above
<code>hasId</code>User must have an ID

The && operator means AND.

Complete Example

Complete Example

html

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

  <h1>JavaScript if Statement</h1>

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

  <script>
    let userName = "John";
    let age = 21;
    let message = "";

    if (age >= 18) {
      message = userName + " is eligible to vote.";
    }

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

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing the value of age.

Example

javascript

let age = 16;

Then check whether the message appears or not.

Important Points

  • The if statement is used to run code only when a condition is true.
  • The condition is written inside parentheses ().
  • The code block is written inside curly braces {}.
  • If the condition is false, the code inside the if block is skipped.
  • Comparison operators are commonly used with if statements.
  • Logical operators can be used to check multiple conditions.
  • Use === for strict comparison in JavaScript.

Conclusion

The JavaScript if statement is used to make decisions in code.

It checks a condition and runs a block of code only when that condition is true.

The if statement is one of the most important parts of JavaScript because it helps create dynamic and interactive programs.

PreviousJavaScript Code BlocksNextJavaScript if else Statement