JavaScript conditional statements are used to make decisions in a program based on given conditions. They control the flow of execution by running different code blocks depending on whether a condition is true or false.
Conditions are evaluated using comparison and logical operators.
They help in building dynamic and interactive applications by responding to different inputs.
How to use the if statement in JavaScript
The if statement is the basic form of a conditional logic in JavaScript. It checks whether a condition is true and runs a block of code if it is.
Types of Conditional Statements
1.if Statement;-
The if statement checks a condition written inside parentheses. If the condition evaluates to true, the code inside {} is executed; otherwise, it is skipped.
Executes code only when a specified condition is true.
Useful for making simple decisions in a program.
Syntax:
let x = 20;
if (x % 2 === 0) {
console.log("Even");
}
if (x % 2 !== 0) {
console.log("Odd");
};
output;-
`Even`
2. if-elseStatement
The if-else statement executes one block of code if a condition is true and another block if it is false. It ensures that exactly one of the two code blocks runs.
Used when there are two possible outcomes.
The else block runs when the if condition is not satisfied.
let age = 25;
if (age >= 18) {
console.log("Adult")
} else {
console.log("Not an Adult")
};
Output
Adult
3. else if Statement
The else if statement is used to test multiple conditions in sequence. It executes the first block whose condition evaluates to true.
Allows checking more than two conditions.
Evaluated from top to bottom until a true condition is found.
const x = 0;
if (x > 0) {
console.log("Positive.");
} else if (x < 0) {
console.log("Negative.");
} else {
console.log("Zero.");
};
Output;
Zero.
Summary
There are different types of conditional statements in JavaScript, just as they are in other programming languages but I will focus on these two:
The most common conditional statement used often is the IF-ELSE statement. just as the name goes, if something is true, do something. otherwise, do something else.
study the image below for reference:
This is the syntax for the if-else statement:
if (condition) {
// If the condition is true, this code block will run
} else {
// If the condition is false, this code block will run
}
In some cases, you might want to check several, related conditions. In those scenarios, you can use an else-if to evaluate the extra conditions.
if (condition) {
// If the condition is true, this code block will run, and code execution
// will stop.
} else if (conditiontwo) {
// If the first condition is false, this code block will run if conditiontwo
// is true
} else if (condition_n) {
// If the previous conditions are both falsy, this code block will run if
// condition_n is true
} else {
// If all conditions are false, this code block will run
}
[`https://miro.medium.com/v2/resize:fit:640/format:webp/1*GDrqEYJgxTHzKbKHaMkUiA.png`](url)
Conditional statements in JavaScript rely on a set of three basic syntax keywords: if, else and else if.
This is the syntax for the if-else statement.






Top comments (0)