Start learning free

LESSON 114 OF 348 · WHILE LOOPS & FOR LOOPS

Why Loops in TypeScript: Repeating Code Without Copying It

Lesson Read the full explanation

Loops are probably the most powerful tool a programming language can offer. You can run the same code multiple times by using a loop.

To really understand their value, let’s take some time to see how frustrating our life would be if a repeated task required us to type out the same code every single time.

Imagine this: A student made a mistake and the teacher asked them to print I made a mistake 5 times to the console.

If we only use console.log(), our program might look like this:

console.log("I made a mistake");
console.log("I made a mistake");
console.log("I made a mistake");
console.log("I made a mistake");
console.log("I made a mistake");

That’s ugly, but still manageable. After all, we’re writing only 5 lines of code and most probably copying and pasting a few times.

Now imagine if we come back to this program and we wanted to print the message for 10, 100, 100000 times? It would take an extremely long time and by the end, we could still end up with inconsistencies and mistakes.

We’ll learn how loops come to our rescue in the next lesson. But for now, let’s gain an appreciation for loops.


Assignment
Follow the Coding Tutorial to see how frustrating our life is without loops.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · WHILE LOOPS & FOR LOOPS

    Why Loops in TypeScript, explained

    The problem loops solve

    A program often has to do the same thing many times: print a message for every student, add up every price, check every row of a table. Without a way to repeat code, the only tool is to write the same statement again and again. A loop is the construct that runs a block of code repeatedly, and it is one of the most powerful tools in any programming language.

    To see why that matters, this lesson first does the job the hard way.

    Repeating by copy and paste

    Imagine you have to print I promise to learn coding. five times. With only console.log, the program is five identical lines:

    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    

    Each call to console.log prints its text followed by a line break, so running the program shows the sentence on five separate lines. It works, and with five lines it is not even hard to write. Typing one line and copying it four times takes seconds.

    Where copying breaks down

    The trouble starts when the requirements change, and they always do:

    • The count grows. If the teacher now wants the sentence 100 times, you need 100 lines. For 100,000 times, the source file is longer than anyone would want to read, let alone edit.
    • The count is not known in advance. If the number of repetitions depends on the data, such as one greeting per name in an array, you cannot know how many lines to write when you write the program.
    • Changes must be repeated. If the sentence needs a different wording, every copy has to be edited. Miss one and the output is inconsistent.
    • Mistakes multiply. With a hundred pasted lines, it is easy to delete one or add one extra without noticing.

    All of these have the same cause: the program says the same thing many times, and the number of times is written out as the number of lines. A better program would say it once and state how many repetitions it wants.

    A first look at the loop version

    TypeScript has two main loops for this, covered in the coming lessons. As a preview, here is the same job with a while loop:

    let counter: number = 1;
    while (counter <= 5) {
        console.log("I promise to learn coding.");
        counter++;
    }
    

    The sentence is written once. A counter starts at 1, the body runs as long as the counter is at most 5, and each pass increases the counter by one. It prints the same five lines. The part that changes the count is the number 5. Making it 100 or 100000 is a one-character edit, and changing the wording is a single edit as well.

    You do not need to understand every detail yet. What matters is the shape: the repeated statement appears once, and a small piece of code around it controls how often it runs.

    The exercise

    The assignment is to print I promise to learn coding. five times using only console.log statements, which is the copy-and-paste way. The goal is to feel how repetitive it is, so that loops will feel like a relief in the next lessons.

    1. Write console.log("I promise to learn coding."); once.
    2. Copy it four more times, so the program has five identical lines.
    3. Check the text. The sentence ends with a full stop, and the lesson's tests compare the output exactly.
    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    console.log("I promise to learn coding.");
    

    Common mistakes

    • Printing the wrong number of lines. Count them. Four or six lines both produce wrong output, and this is exactly the kind of slip that grows worse as the count gets larger.
    • Changing the text slightly. A missing full stop or a different capital letter makes the output differ from what is expected.
    • Printing it all in one call. One console.log with the sentence written five times prints a single long line, not five lines.
    • Forgetting the semicolon or a closing quote. A missing closing quote is a syntax error. A missing semicolon is accepted by TypeScript, but ending statements with one is the common style.

    Key takeaways

    • A loop runs the same code several times without copying it.
    • Repeating by copy and paste does not scale: the number of lines grows with the number of repetitions.
    • Copies are hard to change and easy to get wrong.
    • With a loop, the repeated statement is written once, and the number of repetitions is a value you can change or compute.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free