Mastering If Else Statements and Conditional Logic for Stronger Programming Skills

Table of contents

If else statements
Conditional statements are fundamental components of programming languages, essential for handling logic and decision-making processes. They allow us to control the flow of our code by executing specific blocks of code based on certain conditions. In JavaScript, conditional statements play a crucial role in directing program execution. Let's delve into this foundational topic in JavaScript to understand its significance and explore its practical applications.
Introduction
Conditional statements in JavaScript, such as 'if' and 'if-else', allow us to control the flow of our code based on certain conditions. With these statements, we can specify blocks of code to be executed only when certain conditions are met. For example, using an if statement, we can execute a piece of code if a particular condition evaluates to true. Alternatively, with an if-else statement, we can provide an alternative code block to be executed if the condition evaluates to false. These conditional statements provide powerful tools for building dynamic and responsive programs in JavaScript.
If Statement
In JavaScript, an 'if' statement is a fundamental conditional statement that allows us to execute a block of code only if the specified condition is true. If the condition is false, the code block associated with the 'if' statement is skipped.
Syntax for if statements
index
1
2if (condition){ 3 // Code block to be executed if the condition is true 4}
### **Examples of if statements**
index.js
```cpp
1
2let speed = 87; 3 4if(speed > 90){ 5 console.log("OverSpeeding"); 6} 7 8if(speed > 50 && speed <=90){ 9 console.log("Normal"); 10} 11 12if(speed <= 50){ 13 console.log("Slow"); 14} 15 16// Output: "Normal"
## **If-else Statement**
In JavaScript, an 'if-else' statement is a conditional statement that allows us to execute one block of code if a specified condition evaluates to true, and another block of code if the condition evaluates to false.
### **Syntax for if-else statements**
index.js
```cpp
1
2if (condition) { 3 // Code block to be executed if condition is true 4} else { 5 // Code block to be executed if condition is false 6}
### **Examples of if-else statements**
index.js
```cpp
1
2let myAge = 15; 3 4if(myAge >= 18){ 5 console.log("I can Drive"); 6}else{ 7 console.log("Cannot Drive"); 8} 9 10// Output : "Cannot Drive"
## **If-else-if Statement**
An if-else-if statement is a conditional statement that allows us to test multiple conditions and execute different blocks of code based on the outcome of those conditions. It is an extension of the if-else statement and provides additional flexibility for handling multiple scenarios.
### **Syntax for if-else-if statements**
index.js
```cpp
1
2if (condition1) { 3 // Code block to be executed if condition1 is true 4} else if (condition2) { 5 // Code block to be executed if condition2 is true 6} else { 7 // Code block to be executed if all conditions are false 8}
### **Examples of if-else-if statements**
index.js
```cpp
1
2let weight = 65; 3 4if (weight > 70) { 5 console.log("You are Overweight"); 6} else if (weight > 50 && weight <= 70) { 7 console.log("You are Fit"); 8} else { 9 console.log("You are Underweight"); 10} 11 12// Output : "You are Fit" ```
Subscribe to my newsletter
Read articles from RAJU KUMAR directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
