Table of Contents
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Conditions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The if Statement</h4>
<p id="demo"></p>
<script>
let hour = 12;
if (hour < 18) {
document.getElementById("demo").innerHTML = "Good day!";
}
</script>
</body>
</html>

Result View Example
Use the else statement to specify a block of code to be executed if a condition is false.
If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Conditions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The else Statement</h4>
<p id="demo"></p>
<script>
let hour = 20;
if (hour < 18) {
document.getElementById("demo").innerHTML = "Good day!";
} else {
document.getElementById("demo").innerHTML = "Good evening!";
}
</script>
</body>
</html>

Result View Example
Use the else if statement to specify a new condition if the first condition is false.
If time is less than 10:00, create a "Good morning" greeting, if not but time is less than 20:00, create a "Good day" greeting, otherwise a "Good evening":
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Conditions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The else if Statement</h4>
<p id="demo"></p>
<script>
let time = 14;
if (time < 10) {
document.getElementById("demo").innerHTML = "Good morning!";
} else if (time < 20) {
document.getElementById("demo").innerHTML = "Good day!";
} else {
document.getElementById("demo").innerHTML = "Good evening!";
}
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.