javascript
  1. javascript-if-statement

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

<!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>
Try Playground

Explanation:

In the above example, the If statement checks whether the value of num is greater than 0 or not. If it's true (which is in this case), then the code inside the curly braces is executed. Therefore, the output will be "10 is a positive number".

Use:

If statements is used to execute a code statement based on a certain condition. It allows developers to create programs that can make decisions based on different scenarios.

Important Points:

  • An If statement is a conditional statement which executes code based on a certain condition.
  • If the condition inside the parentheses is true, then the code block inside the curly braces is executed.
  • If the condition is false, then the code block inside the curly braces is skipped.
  • If statement in JavaScript can also include the else statement, which is executed when the condition is false.
  • It is important to be careful while using the If statement, or else the wrong program flow in a script can occur.

Summary:

The If statement is a fundamental construct of programming languages including JavaScript. It allows developers to execute certain code statements based on certain conditions which helps in creating more robust programs. Understanding the syntax, structure, and the how-when to use the If-statement will help you write better JavaScript programs.

Published on: