How to write an if statement in JavaScript if i=5 | Example code

  • by

JavaScript if Conditional statements are used to decide the flow of execution based on conditions. If a condition is true, you can perform one action and if the condition is false, you can perform another action.

if (condition){
    //lines of code to be executed if condition is true
}

How to write an if statement in JavaScript if i=5

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    var i = 5;

    if (i = 5) {
      console.log("The number is", 5);
    }
    else {
      console.log("The number is not 5");
    }

  </script>
</body>
</html> 

Output:

How to write an if statement in JavaScript if i 5

How to write an IF statement in JavaScript?

  1. if i = 5 then
  2. if i == 5 then
  3. if i = 5
  4. if (i == 5)

ANSWER: D

Not equal to in JavaScript if statement

Since a is not equal to 5, it will print true.

let a = 12

if(a!=5){
  console.log(true)
}

Output: true

Do comment if you have any doubts or suggestions on this JS if statement.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.