Open In App

How to use if statement in JavaScript ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Javascript




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


Output

Number is positive.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads