Chuck's Academy

Basic JavaScript

Control Structures

In this chapter, you will learn about control structures in JavaScript. These are fundamental for making decisions in your code and controlling the execution flow. The most common control structures are conditionals and loops, which allow you to execute blocks of code under certain conditions or repeatedly.

Conditionals

Conditional structures allow you to execute different blocks of code based on whether a condition is true or false. The most basic conditional in JavaScript is the if.

If and Else

The if statement evaluates a condition. If it is true, the block of code inside the if is executed. If it is not, you can use an else block to execute alternative code.

javascript
"Here we see an example where a variable age has the value of eighteen. The if block evaluates if age is greater than or equal to eighteen. If true, it prints You are an adult to the console. If not true, it prints You are a minor."

Else If

The else if block allows you to evaluate multiple conditions, executing the code block corresponding to the first true condition.

javascript
"In this example, we have a variable score with the value seventy-five. First, it evaluates if it is greater than or equal to ninety. If not, it evaluates if it is greater than or equal to seventy. If this last one is true, it prints Good job to the console. If neither is true, it prints You can improve."

Switch

Another useful conditional structure is the switch. This structure compares an expression with various possible values (cases) and executes the block of code that matches the value.

javascript
"Here, the variable day has the value three. The switch evaluates the variable and executes the code block corresponding to the value three, printing Wednesday to the console. If there is no match with any value, the default block is executed, in this case, Invalid day."

Loops

Loops allow you to execute a block of code repeatedly while a condition is satisfied or to iterate over a series of elements. JavaScript offers several types of loops: for, while, and do-while.

For Loop

The for loop is one of the most common and allows you to iterate a specific number of times. It consists of three parts: initialization, condition, and final expression.

javascript
"In this for loop, the variable i is initialized with zero. The loop continues executing while i is less than five. After each iteration, i is incremented by one. It prints Iteration followed by the current value of i."

While Loop

The while loop repeats a block of code as long as the evaluated condition is true.

javascript
"In this example, the while loop continues executing while the variable count is less than three. In each iteration, it prints Count is followed by the current value of count, and then increments count by one."

Do-While Loop

The do-while loop is similar to the while, but with a key difference: it always executes the code block at least once since the condition is evaluated after the first execution.

javascript
"Here, the do-while loop executes the code block at least once. In this case, it prints Number is followed by the value of number. Then it increments the variable and continues repeating while the value of number is less than three."

Exception Handling

In JavaScript, you can handle errors and exceptions using try, catch, and finally. This allows you to prevent your application from failing abruptly.

Try and Catch

The try block contains the code that may produce an error, while the catch block is executed if an error occurs. In this way, you can safely handle the exception.

javascript
"In this example, we attempt to divide ten by zero within a try block. If an error occurs, the catch block captures that error and prints a message saying An error occurred followed by the error message."

Finally

The finally block always executes, whether an exception occurs or not. It is useful for releasing resources or performing cleanup tasks.

javascript
"Here, the try block attempts to parse an invalid JSON string. The catch block captures the parsing error and displays the message Error parsing JSON. Finally, the finally block executes showing Execution completed, regardless of whether there was an error or not."

Conclusion

Control structures allow you to write code that can make decisions and repeat actions, which is crucial for the logic of any application. In this chapter, we have explored conditional structures and loops, as well as exception handling to avoid unexpected failures.


Ask me anything