Skip to content
QuiznExam

Control Flow & Loops in JavaScript

Test your knowledge of JavaScript control flow and loops, including if-else statements, switch, for, while, do-while, for...of, for...in, break, and continue.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of control flow statements in JavaScript?

    • A. To control the order in which code is executed
    • B. To create HTML elements only
    • C. To define CSS styles
    • D. To store data in databases
    Show answer

    A. To control the order in which code is executed

    Control flow statements determine which code executes, when it executes, and how many times it executes.

  2. 2. Which statement is used to execute code only when a specified condition is true?

    • A. if
    • B. loop
    • C. repeat
    • D. when
    Show answer

    A. if

    The if statement executes its block when the specified condition evaluates to true.

  3. 3. Which statement provides an alternative block of code when an if condition is false?

    • A. else
    • B. otherwise
    • C. default
    • D. alternative
    Show answer

    A. else

    The else statement provides a block that executes when the preceding if condition is false.

  4. 4. Which JavaScript statement is commonly used when one expression needs to be compared against multiple case values?

    • A. switch
    • B. select
    • C. choose
    • D. match
    Show answer

    A. switch

    The switch statement evaluates an expression and executes code associated with a matching case.

  5. 5. Which keyword is commonly used to stop execution of a switch case and exit the switch statement?

    • A. break
    • B. stop
    • C. exit
    • D. end
    Show answer

    A. break

    The break statement exits the switch statement and prevents execution from continuing into subsequent cases.

  6. 6. Which loop is commonly used when the number of iterations is controlled by initialization, a condition, and an update expression?

    • A. for
    • B. if
    • C. switch
    • D. try
    Show answer

    A. for

    A for loop combines initialization, a condition, and an update expression in its syntax.

  7. 7. Which loop checks its condition before each iteration?

    • A. while
    • B. do...while
    • C. repeat...until
    • D. foreach
    Show answer

    A. while

    A while loop evaluates its condition before executing each iteration, so its body may execute zero times.

  8. 8. Which loop executes its body at least once before checking the condition?

    • A. do...while
    • B. while
    • C. for
    • D. for...of
    Show answer

    A. do...while

    A do...while loop executes its body first and checks the condition afterward, guaranteeing at least one execution.

  9. 9. Which statement immediately terminates the nearest enclosing loop?

    • A. break
    • B. continue
    • C. return
    • D. stop
    Show answer

    A. break

    The break statement terminates the nearest enclosing loop or switch statement.

  10. 10. What does the continue statement do inside a loop?

    • A. Skips the rest of the current iteration and proceeds to the next iteration
    • B. Terminates the loop completely
    • C. Restarts the JavaScript program
    • D. Exits the current function
    Show answer

    A. Skips the rest of the current iteration and proceeds to the next iteration

    The continue statement skips the remaining statements in the current iteration and moves to the next iteration.

  11. 11. Which loop is designed to iterate over the values of an iterable such as an array?

    • A. for...of
    • B. for...in
    • C. for...values
    • D. foreach...of
    Show answer

    A. for...of

    The for...of loop iterates over values produced by an iterable, such as the elements of an array.

  12. 12. Which loop is commonly used to iterate over enumerable property keys of an object?

    • A. for...in
    • B. for...of
    • C. for...keys
    • D. object...in
    Show answer

    A. for...in

    The for...in loop iterates over enumerable property keys of an object.

  13. 13. What is the output of the following code? for (let i = 0; i < 3; i++) { console.log(i); }

    • A. 0 1 2
    • B. 1 2 3
    • C. 0 1 2 3
    • D. 3 2 1
    Show answer

    A. 0 1 2

    The loop starts with i equal to 0 and continues while i is less than 3, so it prints 0, 1, and 2.

  14. 14. Which JavaScript operator can be used as a concise conditional expression with two possible results?

    • A. Ternary operator
    • B. Spread operator
    • C. Comma operator
    • D. Nullish assignment operator
    Show answer

    A. Ternary operator

    The ternary operator uses the form condition ? valueIfTrue : valueIfFalse to choose between two expressions.

  15. 15. Which statement about JavaScript control flow and loops is correct?

    • A. JavaScript supports conditional statements, switch statements, loops, and branching statements
    • B. JavaScript supports only for loops
    • C. Loops cannot be nested in JavaScript
    • D. The break statement can only be used with switch statements
    Show answer

    A. JavaScript supports conditional statements, switch statements, loops, and branching statements

    JavaScript provides several control flow mechanisms, including if-else, switch, for, while, do...while, for...of, for...in, break, and continue.