JavaScript Control Statements | if - if-else if-else Statement - switch Statement in JavaScript

JavaScript Control Statements:

JavaScript control statements are used to manage the flow of a program by making decisions and repeating actions based on conditions. The primary control statements in JavaScript include:

JavaScript Control Statements - if - if-else if-else Statement - switch Statement in JavaScript
JavaScript Control Statements

1. if Statement:

   - The `if` statement is used to execute a block of code if a specified condition evaluates to true.
   javascript
   if (condition) {
     // Code to be executed if the condition is true
   }

2. if-else Statement:

   - The `if-else` statement is used to execute one block of code if a condition is true and another block if the condition is false.
   javascript
   if (condition) {
     // Code to be executed if the condition is true
   } else {
     // Code to be executed if the condition is false
   }

3. if-else if-else Statement:

   - This statement allows you to test multiple conditions and execute different blocks of code based on which condition is true.
   javascript
   if (condition1) {
     // Code to be executed if condition1 is true
   } else if (condition2) {
     // Code to be executed if condition2 is true
   } else {
     // Code to be executed if none of the conditions are true
   }

4. switch Statement:

   - The `switch` statement is used to perform different actions based on different conditions.
   javascript
   switch (expression) {
     case value1:
       // Code to be executed if expression matches value1
       break;
     case value2:
       // Code to be executed if expression matches value2
       break;
     default:
       // Code to be executed if none of the cases match
   }

5. for Loop:

   - The `for` loop is used to iterate over a block of code a specified number of times.
   javascript
   for (initialization; condition; iteration) {
     // Code to be executed in each iteration
   }
   

6. while Loop:

   - The `while` loop is used to repeatedly execute a block of code as long as a specified condition is true.
   javascript
   while (condition) {
     // Code to be executed while the condition is true
   }
   

7. do-while Loop:

   - The `do-while` loop is similar to the `while` loop, but it ensures that the code block is executed at least once before checking the condition.
   javascript
   do {
     // Code to be executed at least once
   } while (condition);
   

8. for...in Loop:

   - The `for...in` loop is used to iterate over the properties of an object.
   javascript
   for (variable in object) {
     // Code to be executed for each property of the object
   }
   

9. for...of Loop:

   - The `for...of` loop is used to iterate over the values of an iterable object (arrays, strings, etc.).
   javascript
   for (variable of iterable) {
     // Code to be executed for each value of the iterable
   }
   

These control statements provide the foundation for managing program flow and implementing various logic structures in JavaScript. They are essential for creating dynamic and responsive applications.

ShowHideComments