Back to JavaScript
2025-12-015 min read

Decision Making and Loop

Learn Decision Making and Loop step by step with clear examples and exercises.

Title: Mastering Decision Making and Loops in JavaScript - A full guide

Why This Matters

Understanding decision making and loops is crucial for any JavaScript developer. These concepts enable you to create dynamic, flexible, and efficient code. They are essential for building complex applications, solving real-world problems, and even debugging your own code. In this lesson, we'll delve into the world of decision making and loops in JavaScript, providing practical examples, common mistakes, and tips to help you master these fundamental concepts.

Prerequisites

To get the most out of this lesson, you should have a basic understanding of the following:

  • JavaScript syntax and variables
  • Basic data types (numbers, strings, booleans)
  • Functions and control structures (if/else statements)

Core Concept

Decision Making in JavaScript

Decision making in JavaScript is all about controlling the flow of your code based on certain conditions. The most common decision-making structure is the if/else statement, which allows you to test a condition and execute different blocks of code depending on whether the condition is true or false.

let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote yet.");
}

In this example, we're checking if the age variable is greater than or equal to 18. If it is, we print "You are eligible to vote." Otherwise, we print "You are not eligible to vote yet."

Loops in JavaScript

Loops allow you to repeat a block of code as long as a certain condition is met. JavaScript provides two main types of loops: for and while.

For Loop

The for loop is used when you know exactly how many times you want to iterate through a loop. It consists of three parts: the initialization, the condition, and the increment/decrement.

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

In this example, we're using a for loop to print numbers from 0 to 9. The variable i is initialized to 0, the condition checks if i is less than 10, and the increment increments i by 1 each iteration.

While Loop

The while loop continues executing as long as a certain condition remains true. It consists of two parts: the initialization and the condition.

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

This example is equivalent to the for loop example above, but it uses a while loop instead. The loop continues as long as i is less than 10, and each iteration increments i.

Break and Continue Statements

The break statement allows you to exit a loop prematurely, while the continue statement skips the current iteration and moves on to the next one.

for (let i = 0; i < 10; i++) {
if (i === 5) {
console.log("Skipping 5...");
continue;
}
console.log(i);
}

In this example, we're using a for loop to print numbers from 0 to 9. When the number 5 is encountered, the continue statement skips that iteration and moves on to the next one.

Worked Example

Let's create a simple JavaScript program that calculates the factorial of a number using loops.

function factorial(n) {
let result = 1;
for (let i = n; i > 1; i--) {
result *= i;
}
return result;
}

console.log(factorial(5)); // Output: 120

In this example, we've defined a function called factorial that takes an integer n as its argument and calculates its factorial using a for loop. The variable result is initialized to 1, and the loop multiplies result by each number from n down to 1, storing the result in result.

Common Mistakes

  1. ### Forgetting to initialize a loop counter
for (let i; i < 10; i++) {
console.log(i); // Uncaught ReferenceError: i is not defined
}

In this example, we've forgotten to initialize the i variable before using it in our loop condition. This results in a ReferenceError.

  1. ### Using an uninitialized variable in a condition
for (let i; i > 10; ) {
console.log(i); // Uncaught ReferenceError: i is not defined
}

In this example, we've used the i variable in our loop condition without initializing it, resulting in another ReferenceError.

  1. ### Misusing break and continue statements
for (let i = 0; i < 10; ) {
if (i === 5) {
console.log("Exiting loop...");
break; // Exits the loop prematurely
}
console.log(i);
i++;
}

In this example, we've used the break statement to exit the loop prematurely when the number 5 is encountered. However, if we forget to increment i, the loop will never end, resulting in an infinite loop.

Practice Questions

  1. Write a JavaScript program that prints the numbers from 1 to 20 using a for loop.
  2. Modify the factorial function from the worked example to handle negative numbers and return "Invalid input" if the number is not an integer.
  3. Write a JavaScript program that finds the largest prime number less than or equal to 50 using a while loop.

FAQ

--

### What happens when I use a non-integer value in a for loop?

In JavaScript, if you use a non-integer value (such as a string or float) in a for loop, the loop will behave unexpectedly. It's best to ensure that your loop counter is always an integer.

### Can I use a do...while loop in JavaScript?

Yes, you can use a do...while loop in JavaScript. The syntax is as follows:

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

In this example, the body of the do...while loop executes at least once before the condition is checked, unlike a traditional while loop.

Decision Making and Loop | JavaScript | XQA Learn