Open In App

How to use if statement in JavaScript ?

An if statement in JavaScript is used for conditional execution. It evaluates a condition inside parentheses and executes a block of code if the condition is true.

Example: Here, the if statement checks whether the variable num is greater than 0. If the condition num > 0 evaluates to true, the code block inside the curly braces (console.log("Number is positive.")) is executed, and it prints “Number is positive.” to the console.




let num = 10;
 
if (num > 0) {
    console.log("Number is positive.");
}

Output
Number is positive.
Article Tags :