DEV Community

Cover image for Conditional Statements In Javascript.
Chandru P
Chandru P

Posted on

Conditional Statements In Javascript.

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

 (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4do41werfv61a0h5k1b3.png)

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.

(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evw7g6b9dp38ke1904lw.png)

  • 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");
};
Enter fullscreen mode Exit fullscreen mode

output;-

  `Even` 
Enter fullscreen mode Exit fullscreen mode

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.

 (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhz2poxn0kg65lpp99fy.png)

  • 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")
};
Enter fullscreen mode Exit fullscreen mode

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.

 (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ud57qrjudqyy2h4rqijl.png)

  • 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.");
};
Enter fullscreen mode Exit fullscreen mode

Output;
Zero.
Summary

 (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfhkws7izsw2auvms7og.png)<br>

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
     [`https://miro.medium.com/v2/resize:fit:640/format:webp/1*GDrqEYJgxTHzKbKHaMkUiA.png`](url)                 
Enter fullscreen mode Exit fullscreen mode

Conditional statements in JavaScript rely on a set of three basic syntax keywords: if, else and else if.

(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf74tmvm8dgsmfki4rlj.png)<br>

This is the syntax for the if-else statement.

Top comments (0)