JavaScript Flow Control

Flow control mechanism in JavaScript.

JavaScript

Flow Control

Flow control in JavaScript is essential for managing the execution order of code. It allows you to make decisions, iterate over data, and execute code conditionally. There are several mechanisms for flow control in JavaScript, including:

Conditional Statements:

Conditional statements allow you to execute different blocks of code based on whether a specified condition evaluates to true or false. In JavaScript, the primary conditional statements are 'if', 'else if', and 'else'.

js Copy Code
let num = 10;

if (num > 0) {
    console.log("Number is positive");
} else if (num < 0) {
    console.log("Number is negative");
} else {
    console.log("Number is zero");
}
Output:
Number is positive

Loops:

Loops are used to repeatedly execute a block of code until a certain condition is met. JavaScript supports different types of loops such as 'for', 'while', and 'do...while'.

[for]

js Copy Code
// Using a for loop to iterate from 0 to 4
for (let i = 0; i < 5; i++) {
    console.log("for loop " + i);
}
Output:
for loop 0
for loop 1
for loop 2
for loop 3
for loop 4

[while]

js Copy Code
// Using a while loop to iterate until condition is false
let x = 0;
while (x < 5) {
    console.log("while loop " + x);
    x++;
}
Output:
while loop 0
while loop 1
while loop 2
while loop 3
while loop 4

[do...while]

js Copy Code
// Using a do...while loop to ensure the code block executes at least once
let y = 0;
do {
    console.log("do-While loop " + y);
    y++;
} while (y < 5);
Output:
do-While loop 0
do-While loop 1
do-While loop 2
do-While loop 3
do-While loop 4

Break and Continue:

The 'break' statement helps to terminates the current loop, switch, or label statement, and the 'continue' statement skips the current iteration of a loop and continues with the next iteration.

[break]

js Copy Code
for (let i = 0; i < 5; i++) {
    if (i === 3) {
        break;
    }
    console.log(i);
}
Output:
0
1
2

[continue]

js Copy Code
for (let i = 0; i < 5; i++) {
    if (i === 3) {
        continue;
    }
    console.log(i);
}
Output:
0
1
2
4

Exception Handling:

Exception handling allows you to deal with errors or exceptional situations in your code gracefully. JavaScript provides 'try', 'catch', and 'finally' blocks for exception handling.

js Copy Code
try {
    // Code that may throw an exception
    throw new Error('An error occurred');
} catch (error) {
    // Handling the exception
    console.error('Caught an error:', error.message);
} finally {
    // Code that will always execute
//regardless of whether an exception occurred
    console.log('This will always execute');
}
Output:
ERROR!
Caught an error: An error occurred
This will always execute

Async and Await:

The 'async' and 'await' are features in JavaScript used for managing asynchronous operations, making it easier to handle tasks like fetching data from servers or executing time-consuming computations without blocking the main thread.

js Copy Code
async function fetchData() {
    try {
        let response = await fetch('https://api.example.com/data');
        let data = await response.json();
        console.log(data);
    } catch (error) {
        console.error('Error fetching data:', error);
    }
}

fetchData();

* These are some of the core mechanisms for flow control in JavaScript. Understanding these concepts is essential for writing efficient and maintainable code.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.