Javascript If statement

Gowtham
4 min readFeb 13, 2023

if statement

The if statement in JavaScript is a conditional statement that allows you to execute a certain block of code only if a certain condition is met. It is used to make decisions in your code based on whether a certain condition is true or false.

The Flow of the `if` statement is as follows:

The syntax for an if statement is as follows:

if (condition) {
// code to be executed if condition is true
}

Here, condition is a boolean expression that evaluates to either true or false. If the condition is true, the code inside the if block will be executed. If the condition is false, the code inside the if block will be skipped.

Here is an example to illustrate the use of the if statement:

var x = 10;

if (x > 5) {
console.log("x is greater than 5");
}

Output:

if…else

The if...else statement in JavaScript is a conditional statement that allows you to execute a certain block of code if a certain condition is met, and another block of code if the condition is not met. It is used to make decisions in your code based on whether a certain condition is true or false.

The Flow of the `if…else` statement is as follows:

The syntax for an if...else statement is as follows:

if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}

Here, condition is a boolean expression that evaluates to either true or false. The code inside the if block will be executed if the condition is true, and the code inside the else block will be executed if the condition is false. If you only want to execute some code if a certain condition is true, and do nothing if it is false, you can omit the else block.

Here is an example to illustrate the use of the if...else statement:

var x = 10;

if (x > 5) {
console.log("x is greater than 5");
} else {
console.log("x is not greater than 5");
}

Output:

In this example, the condition x > 5 is true, so the code inside the if block will be executed, and the output will be “x is greater than 5”.

if-else-if

The if…else if…else statement in JavaScript is a conditional statement that allows you to specify multiple conditions and execute different blocks of code based on the conditions being met. It is used to make complex decisions in your code based on multiple conditions.

The syntax for an if...else...ifstatement is as follows:

The syntax for an if…else if…else statement is as follows:

if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else if (condition3) {
// code to be executed if condition1 and condition2 are false and condition3 is true
} ...
else {
// code to be executed if all conditions are false
}

Here, each condition is a boolean expression that evaluates to either true or false. If condition1 is true, the code inside the first if block will be executed, and the rest of the conditions will be skipped. If condition1 is false and condition2 is true, the code inside the second if block will be executed, and the rest of the conditions will be skipped. The process continues until a condition is met, and the corresponding code block is executed. If all conditions are false, the code inside the else block will be executed.

Here is an example to illustrate the use of the if…else if…else statement:

var x = 10;

if (x > 15) {
console.log("x is greater than 15");
} else if (x > 5) {
console.log("x is greater than 5 but not greater than 15");
} else {
console.log("x is not greater than 5");
}

Output

In this example, the first condition x > 15 is false, the second condition x > 5 is true, so the code inside the second if block will be executed, and the output will be “x is greater than 5 but not greater than 15”.

Check out my Recent blog post 👇

Javascript Operators

Javascript Global Variable

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Gowtham
Gowtham

No responses yet

Write a response