TechMentorAI

Control flow

in JavaScript

Published

Control flow is the order in which individual statements, instructions, or function calls are executed within a script. JavaScript features various control flow structures including: conditions (if, else, else if), loops (for, while), and exceptions.

Imagine Control flow in JavaScript

Conditions in JavaScript

A condition is a mechanism of decision-making in JavaScript. 'if' statement is used to specify a block of code to be executed if a condition is true. 'else' statement is used to specify a block of code to be executed if the same condition is false. 'else if' statement is used to specify a new condition to test if the first condition is false.

if (condition) {
  // block of code to be executed if the condition is true
} else if (anotherCondition) {
  // block of code to be executed if the condition is false and 'anotherCondition' is true
} else {
  // block of code to be executed if the condition is false and 'anotherCondition' is also false
}

Loops in JavaScript

A loop is a sequence of instructions that is continually repeated until a certain condition is reached. A 'for' loop repeats until a specified condition evaluates to false. The JavaScript 'for' loop is much more flexible than it is in most other languages, supporting both traditional styles of use and other, JavaScript-specific ones. The 'while' loop executes a block of code as long as a specified condition is true.

for (let i = 0; i < 10; i++) {
  console.log(i);
}

let i = 0;
while (i < 10) {
  console.log(i);
  i++;
}

Exceptions in JavaScript

An exception is an unusual condition requiring special handling. In JavaScript, we use 'try', 'catch', and 'finally' statements to implement exception handling. When an exception is thrown in the 'try' block, the exception is caught and handled in the 'catch' block. And the 'finally' block is used to execute code regardless of the result of the try...catch block.

try {
  // trying to execute some code
} catch (error) {
  // handling error if above block fails
  console.log(error);
} finally {
  // code to be executed regardless of the try / catch result
}

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.21 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

Just like how you decide whether to have cereal or pancakes for breakfast, computers also have to make decisions. Control flow in JavaScript is like the computer's recipe for making those decisions. It helps the computer decide what to do next.

Imagine Control flow in JavaScript

If Statements in JavaScript

Think about when your mommy and daddy say, 'If you finish your vegetables, then you can have dessert'. In the computer's language, JavaScript, this is called an 'if statement'. The computer checks if something is true (like you finishing your vegetables), and if it's true, it does a certain thing (like giving you dessert).

if (youAteYourVegetables) {
   giveDessert();
}

Else Statements in JavaScript

What happens if you don't finish your vegetables? Maybe you won't get dessert. That's the 'else' part. In JavaScript, it's written as 'else'. It tells the computer what to do if the first thing it checked (like you eating your vegetables) isn't true.

if (youAteYourVegetables) {
   giveDessert();
} else {
   noDessert();
}

For Loops in JavaScript

Imagine you're putting away your toys. You do the same thing for each toy: pick it up and put it in the toy box. In JavaScript, this is called a 'for loop'. It tells the computer to do the same thing over and over (like pick up a toy and put it in the box) until something tells it to stop (like when there are no more toys).

for (let toy of toys) {
   putInToyBox(toy);
}

While Loops in JavaScript

A 'while loop' is like playing a game of tag. You keep running 'while' you're still 'It'. In JavaScript, we can tell the computer to keep doing a task 'while' a certain condition is true.

while (youAreIt) {
   keepRunning();
}

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.31 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

To understand how control flow can be used in more complex scenarios such as asynchronous operations, you can check Understanding Promises in JavaScript

About author

Roman Y.
Senior Software Engineer at Nike

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email