WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

JavaScript · Basic · question 6 of 100

What are the various types of loops in JavaScript, and how do they work?

📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

In JavaScript, loops are used to execute a block of code repeatedly. There are three types of loops in JavaScript: for, while, and do-while. Each of these loops has its own syntax and use cases.

for loop: The for loop is used to execute a block of code a specific number of times. It consists of three parts: the initialization, the condition, and the increment/decrement.

Syntax:

    for (initialization; condition; increment/decrement) {
        // Code to be executed
    }

Example:

    for (var i = 0; i < 5; i++) {
        console.log(i);
    }
    
    // Outputs:
    // 0
    // 1
    // 2
    // 3
    // 4

while loop: The while loop is used to execute a block of code while a condition is true. It checks the condition before executing the code, and repeats the process until the condition becomes false.

Syntax:

    while (condition) {
        // Code to be executed
    }

Example:

    var i = 0;
    while (i < 5) {
        console.log(i);
        i++;
    }
    
    // Outputs:
    // 0
    // 1
    // 2
    // 3
    // 4

do-while loop: The do-while loop is similar to the while loop, but it executes the code at least once before checking the condition. This ensures that the code is executed at least once, even if the condition is false from the beginning.

Syntax:

    do {
        // Code to be executed
    } while (condition);

Example:

    var i = 0;
    do {
        console.log(i);
        i++;
    } while (i < 5);
    
    // Outputs:
    // 0
    // 1
    // 2
    // 3
    // 4

In addition to these three types of loops, there is also the for...in loop, which is used to iterate over the properties of an object, and the for...of loop, which is used to iterate over the elements of an iterable object like an array.

Example:

    var person = {
        firstName: 'John',
        lastName: 'Doe',
        age: 30
    };
    
    for (var key in person) {
        console.log(key + ': ' + person[key]);
    }
    
    // Outputs:
    // firstName: John
    // lastName: Doe
    // age: 30

Example:

    var fruits = ['apple', 'banana', 'orange'];
    
    for (var fruit of fruits) {
        console.log(fruit);
    }
    
    // Outputs:
    // apple
    // banana
    // orange

These are the various types of loops in JavaScript and how they work. As a JavaScript expert, I would be happy to provide more information or examples if needed.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview — then scores it.
📞 Practice JavaScript — free 15 min
📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

All 100 JavaScript questions · All topics