JavaScript Control Flow: Conditionals & Loops Challenge — Questions & Answers

Test your expertise in JavaScript conditional statements, loops, and control flow interrupts with these challenging questions designed to assess nuanced understanding.

This quiz contains 16 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.

  1. Question 1: Nested Conditional Logic

    Given the code snippet: let x = 10; if (x > 5) { if (x < 15) { x++; } } else { x = 0; }, what will be the value of x after execution?

    • 15
    • 10
    • 0
    • 11
    • 9
    Show correct answer

    Correct answer: 11

  2. Question 2: Strict vs. Loose Comparison

    In the statement if ('0' == 0), which principle is applied in JavaScript's conditional control flow?

    • No comparison is made due to syntax error
    • Strict comparison makes the condition false
    • Boolean logic is ignored
    • Type coercion occurs and the condition is true
    • Type must match exactly
    Show correct answer

    Correct answer: Type coercion occurs and the condition is true

  3. Question 3: Switch Statement Fallthrough

    Given: let val = 2; switch(val) { case 1: val += 1; case 2: val += 2; break; case 3: val += 3; }, what is the value of val after executing the switch?

    • 6
    • 5
    • 7
    • 3
    • 4
    Show correct answer

    Correct answer: 4

  4. Question 4: Break Statement in Loops

    What will be logged by: let output = ''; for(let i=0; i<5; i++) { if(i===3) break; output += i; } console.log(output);

    • 1234
    • 3
    • 012
    • 0123
    • 01
    Show correct answer

    Correct answer: 012

  5. Question 5: Continue in a While Loop

    Which value will NOT be printed by the following? let i = 0; while(i < 5) { i++; if(i === 3) continue; console.log(i); }

    • 4
    • 5
    • 3
    • 2
    • 1
    Show correct answer

    Correct answer: 3

  6. Question 6: For...in Loop on Arrays

    Consider let arr = ['a', 'b']; arr.extra = 'c'; Using for (let prop in arr), which values can prop take?

    • 'a', 'b', 'c'
    • 'array', 'object', 'extra'
    • '0', '1', 'extra'
    • 'arr', 'extra'
    • '0', '1'
    Show correct answer

    Correct answer: '0', '1', 'extra'

  7. Question 7: For...of Loop with Sparse Arrays

    Given let arr = [1,,3]; using for (let num of arr), how many iterations will occur and what values will be assigned to num?

    • 2 iterations: 1 and 3
    • 2 iterations: 1 and undefined
    • 3 iterations: 1, undefined, 3
    • 3 iterations: 1, null, 3
    • 1 iteration: [1, 3]
    Show correct answer

    Correct answer: 2 iterations: 1 and 3

  8. Question 8: Switch Default Case Placement

    What happens if the default case in a switch statement is placed before the first case label?

    • Only the default runs regardless of other cases
    • It causes a syntax error
    • It is valid and executes if no case matches
    • The switch statement is ignored
    • The default executes for every case
    Show correct answer

    Correct answer: It is valid and executes if no case matches

  9. Question 9: Short-Circuit Evaluation

    In the expression if (false && someFunction()), what is guaranteed about the execution of someFunction()?

    • It throws an error
    • It will always be called
    • It only runs if false is true
    • It returns undefined
    • It will not be called
    Show correct answer

    Correct answer: It will not be called

  10. Question 10: Loop Scope and Variable Hoisting

    Why does the following code log 5, 5, 5, 5, 5? for(var i=0; i<5; i++){ setTimeout(()=>console.log(i),0); }

    • Each timeout captures a unique value
    • The loop doesn’t increment i
    • let should be used for block scoping
    • Because 'var' shares function scope and i becomes 5 after the loop
    • setTimeout delays are synchronous
    Show correct answer

    Correct answer: Because 'var' shares function scope and i becomes 5 after the loop

  11. Question 11: Do...While vs. While Conditions

    Which statement best describes the difference between do...while and while loops in evaluating loop conditions?

    • do...while never executes if condition is false
    • while loops always execute body twice
    • while guarantees single execution before checking
    • do...while executes its body at least once regardless of condition
    • do...while is only for asynchronous code
    Show correct answer

    Correct answer: do...while executes its body at least once regardless of condition

  12. Question 12: Combined Conditional Expression

    What is returned by: if (0 || (null && true)) { return 'A'; } else { return 'B'; }

    • 0
    • null
    • undefined
    • 'B'
    • 'A'
    Show correct answer

    Correct answer: 'B'

  13. Question 13: Switch Statement Case Expression Types

    If a switch statement's case expressions are objects, under what circumstance will any case match?

    • Matches by JSON stringification
    • If objects have identical properties
    • Always matches by value
    • Only if the same object reference is compared
    • Never matches anything
    Show correct answer

    Correct answer: Only if the same object reference is compared

  14. Question 14: For Loop with Multiple Conditions

    What is the result of this code: let res = ''; for(let i=2,j=4; i<5&&j>1; i++,j--){ res += i+j; } return res;

    • '65' (with a space)
    • '65'
    • '69'
    • '710'
    • '911'
    Show correct answer

    Correct answer: '65'

  15. Question 15: Continue in Nested Loops

    How many times does Hello print in: let count=0; for(let i=0;i<3;i++){ for(let j=0;j<3;j++){ if(j==1) continue; count++; } } console.log('Hello'.repeat(count));

    • 8
    • 9
    • 6
    • 3
    • 12
    Show correct answer

    Correct answer: 6

  16. Question 16: For...of with Strings

    Given let s = 'abc', what is the output of: let result = ''; for(let c of s){ result += c.toUpperCase(); } return result;

    • 'abc'
    • 'A B C'
    • 'ABC'
    • 'aBc'
    • undefined
    Show correct answer

    Correct answer: 'ABC'