JavaScript If Statement
JavaScript If statement is widely used in programming to execute certain code statements based on certain conditions.
Syntax:
An If statement in JavaScript uses the following syntax:
if (condition) {
// code block to be executed if the condition is true
}
The if statement is followed by a set of parentheses that contain a condition. The code block enclosed in curly braces is executed only if the condition inside parentheses is true.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Positive Number Example</title>
</head>
<body>
<!-- Placeholder for displaying the output -->
<p id="output"></p>
<script>
// Assign a value to the variable 'num'
var num = 10;
// Check if 'num' is a positive number
if (num > 0) {
// Display the result in the HTML document
document.getElementById("output").innerHTML = num + " is a positive number";
}
</script>
</body>
</html>
}
Output:
10 is a positive number