if statement in JavaScript
An if statement in JavaScript is a control flow statement that allows you to execute a block of code only if a specified condition is true.
Syntax:
if (condition) {
// code to be executed if condition is true
}
In this syntax, a condition is an expression that returns a Boolean value of true or false. If the condition is true, the code inside the curly braces {} is executed. If the condition is false, the code inside the if block is not executed.
The if statement in JavaScript is used to make decisions in your code, and it's one of the most basic and widely used control flow statements in JavaScript. It allows you to control the flow of execution of your code based on whether certain conditions are met.
Example:
Output:
In the above example, the user is prompted to enter their age, which is then stored in the age variable. The if statement checks if the age is greater than or equal to 18. If the condition is true, the message "You are eligible to vote." is printed on the alert box. If the condition is false, the code inside the if block is not executed.
No comments:
Post a Comment