KodingKaro
JavaScript · Beginner · Loops

Repeating work safely

This lesson applies Loops concepts to code you can run and adapt.

✓ What you will learn in this topic

  • Explain key Loops concepts.
  • Read and adapt the examples.
  • Apply the idea to a small real interface or data task.

Concept explanation

for, while, do...while, break, continue, nested loops, for...of, for...in, and choosing a loop.

These concepts appear in everyday interfaces and data transformations; understanding the trade-offs helps you choose a reliable implementation.

Syntax

The examples use standard ECMAScript syntax. Run console examples in a browser console or Node.js; DOM examples require a browser page containing the referenced elements.

Example 1: a small experiment

Run this compact example to observe the core rule before applying it to a larger feature.

for (const day of ['Mon', 'Tue']) console.log(day);

Expected output: Mon, then Tue

Why it is useful: A small experiment makes the value or behavior easy to inspect.

Example 2: a practical feature

This example models a small piece of application behavior. Adapt its values and inspect what changes.

const prices = [4, 8, 3];
let total = 0;
for (const price of prices) total += price;
console.log(total);

Expected output: 15

Why it is useful: The same pattern can be extended into a real feature without tying the entire application to one large block of code.

Common mistakes

for...in visits enumerable property keys and is not the usual way to iterate array values.

Best practices

Choose for...of for iterable values, for...in for object keys, and an array method when transforming or filtering data.

Practice exercise

Calculate the total of a basket while skipping one item marked unavailable.

Lesson summary

for, while, do...while, break, continue, nested loops, for...of, for...in, and choosing a loop.

Knowledge check

Which statement best describes an important idea in Loops?

Show explanation

for, while, do...while, break, continue, nested loops, for...of, for...in, and choosing a loop.

Practical example

javascript
const prices = [4, 8, 3];
let total = 0;
for (const price of prices) total += price;
console.log(total);

Expected result:

15

Hands-on practice task

Calculate the total of a basket while skipping one item marked unavailable.

In this section

Other lessons in Loops

Broaden your knowledge

Related learning topics