Control Flow in JavaScript: If, Else, and Switch Explained
JavaScript control flow or statement is used to control the execution of a program based on a specific condition. In this flow provide a conditional code therefore different task different code execute.
What control flow means in programing ?
Control flow mean in programing like run code based on condition. It mean control flow or statement is used to control the execution of a program based on a specific condition.
Types of Control Statements in JavaScript :
The conditional statement are used for decision making, a decision.
If Statement
if-Else statement
Switch statement
Ternary Operator
For loop
While loop
Do - while loop
The if statement :
Use the if statement to execute a statement of a logical condition true. Mean user provide condition, condition is true then if block code execute otherwise continue program.
syntax
if ( condition_is_given_here ) {
// If the condition is met,
//the code will get executed.
}
Example of if statement :
const num = 5;
if (num > 0) {
console.log("The number is positive.");
};
The if-else statement :
Use the if-else statement to execute a statement if a logical condition is true then execute if block code otherwise execute else block code.
Syntax :
if ( condition_is_given_here ) {
// If the condition is met,
//the code will get executed.
} else {
// code
}
Example if else statement :
let num = -10;
if (num > 0)
console.log("The number is positive.");
else
console.log("The number is negative");
The else if ladder :
The else if ladder in a JavaScript control flow using multiple condition it mean user use else if structure and define multiple condition then execute code check first condition it condition is false then check second condition and so. on check all condition after that execute else block code.
syntax :
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
}
Example else if ladder :
if (a>10) {
console.log(a)
// code
} else if(a>20){
console.log(a)
// code
}else if(a>30){
console.log(a)
// code
}else {
console.log(a)
// code
}
The Switch Statement :
The switch case statement used also decision making purpose because it provide multiple case then case is matched then that code part execute otherwise last default case execute automatically.
It provide easier syntax use this syntax make control statement based on multiple case. using the switch case statement is seen to be more convenient than if-else statements
syntax :
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
Example Switch case :
let num = 5;
switch (num) {
case 0:
console.log("Number is zero.");
break;
case 1:
console.log("Nuber is one.");
break;
case 2:
console.log("Number is two.");
break;
default:
console.log("Number is greater than 2.");
};
When to use switch vs if - else :
Use if - else :
When conditions is complex or different type then use if - else. Have a multiple condition, check range and have a logical operations.
Use Switch :
When one variable compare multiple fixed value then use switch case. In this check exact match, clean and readable code and same variable for multiple case.
I hope this blog very helpful for you. Thanks for reading this blog.