Affiliate link
Node.js - Controls
Control statements are used to control the flow of execution of a program based on certain conditions. Control statements allow you to make decisions and execute different blocks of code based on those decisions.
Types of control loops that you can use in Node.js
If Statements
If statements are used when you want to execute a block of code based on a certain condition.
if (condition) {
// code to be executed if condition is true
}let num = 5;
if (num % 2 === 0) {
console.log("The number is even.");
} else {
console.log("The number is odd.");
}Switch Statements
Switch statements are used when you want to execute different blocks of code based on the value of a variable.
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
default:
// code to be executed if expression doesn't match any case
break;
}let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
break;
}Try-Catch Statements
Try-catch statements are used when you want to handle errors or exceptions in your code.
try {
// code that might throw an exception
} catch (error) {
// code to be executed if an exception is thrown
}let num1 = 5;
let num2 = 0;
try {
let result = num1 / num2;
console.log(result);
} catch (error) {
console.log("Error: " + error.message);
}Affiliate link
Affiliate link





