Javascript

06 If Else Statement

Here are five examples of `if-else` statements in JavaScript, ranging from easy to hard, with output displayed directly in the browser using `document.write`.


 

---


 

### 1. **Simple If-Else** - Easy
```javascript
document.write("<h3>Simple If-Else</h3>");
let age = 20;
if (age >= 18) {
    document.write("You are an adult.");
} else {
    document.write("You are a minor.");
}
```


 

---


 

### 2. **If-Else with Multiple Conditions** - Easy
```javascript
document.write("<h3>If-Else with Multiple Conditions</h3>");
let temperature = 25;
if (temperature > 30) {
    document.write("It's hot outside.");
} else if (temperature < 15) {
    document.write("It's cold outside.");
} else {
    document.write("The weather is moderate.");
}
```


 

---


 

### 3. **Nested If-Else** - Medium
```javascript
document.write("<h3>Nested If-Else</h3>");
let marks = 85;
if (marks >= 50) {
    if (marks >= 80) {
        document.write("You passed with distinction!");
    } else {
        document.write("You passed.");
    }
} else {
    document.write("You failed.");
}
```


 

---


 

### 4. **If-Else with Logical Operators** - Medium
```javascript
document.write("<h3>If-Else with Logical Operators</h3>");
let hasID = true;
let isVIP = false;
if (hasID && isVIP) {
    document.write("Welcome to the VIP section.");
} else if (hasID) {
    document.write("You can enter the general area.");
} else {
    document.write("Access denied.");
}
```


 

---


 

### 5. **Complex If-Else with Multiple Scenarios** - Hard
```javascript
document.write("<h3>Complex If-Else with Multiple Scenarios</h3>");
let score = 78;
if (score >= 90) {
    document.write("Grade: A");
} else if (score >= 80) {
    document.write("Grade: B");
} else if (score >= 70) {
    document.write("Grade: C");
} else if (score >= 60) {
    document.write("Grade: D");
} else {
    document.write("Grade: F");
}
```


 

---


 

Copy and paste these examples into an HTML file to see them displayed in your browser.
2 min read
Nov 19, 2024
By Md Real
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Nov 19, 2024 • 2 min read
08 For Loop
Nov 19, 2024 • 2 min read
03 Js Variable
Nov 19, 2024 • 2 min read
02 Js Comment