JavaScript Loops MCQs

1 min read

Iteration structures in JavaScript allow developers to execute code blocks repeatedly based on dynamic boolean conditions. Standard loops such as for, while, and do...while handle basic numerical iteration and pre/post-condition checks. ECMAScript 2015 (ES6) introduced specialized iteration mechanisms: for...in for enumerating non-Symbol object property keys, and for...of for traversing iterable collections like Arrays, Strings, Sets, and Maps. Mastering loop control statements (break, continue, and labeled statements), along with understanding closure scoping behaviors when using let versus var in iteration headers, is essential for writing efficient runtime code and excelling in technical interviews.

1. What is the output of the following loop? let str = ""; for (let i = 0; i < 4; i += 2) { str += i; } console.log(str);

a) 0123
b) 02
c) 024
d) 24
Correct Answer: b) 02
Explanation:
The variable i starts at 0 and increments by 2 on each iteration (i = 0, i = 2). When i becomes 4, the condition i < 4 evaluates to false, leaving str as '02'.

2. What will be printed to the console? let x = 5; while (x > 5) { console.log(x); x--; }

a) 5
b) 5 4 3 2 1
c) No output
d) Undefined
Correct Answer: c) No output
Explanation:
The initial condition x > 5 is false because 5 > 5 is false. The body of the while loop never executes.

3. What will be printed to the console? let x = 5; do { console.log(x); x--; } while (x > 5);

a) 5
b) No output
c) 5 4
d) Infinite loop
Correct Answer: a) 5
Explanation:
A do...while loop runs the loop body once before evaluating the condition. 5 is printed, x becomes 4, and then 4 > 5 evaluates to false, terminating the loop.

4. What is the output of the following code snippet? const obj = { a: 1, b: 2, c: 3 }; let keys = ""; for (const key in obj) { keys += key; } console.log(keys);

a) 123
b) abc
c) a1b2c3
d) undefined
Correct Answer: b) abc
Explanation:
The for...in loop iterates over the enumerable property names (keys) of an object. Concatenating them results in 'abc'.

5. What is the output when executing this code? const set = new Set([1, 2, 2, 3]); let sum = 0; for (const val of set) { sum += val; } console.log(sum);

a) 8
b) 6
c) 5
d) 3
Correct Answer: b) 6
Explanation:
Sets only store unique values, so the Set contains [1, 2, 3]. Iterating over these values with for...of gives 1 + 2 + 3 = 6.

6. What will be logged by the following code? let count = 0; for (let i = 0; i < 3; i++) { if (i === 1) continue; count += i; } console.log(count);

a) 3
b) 2
c) 0
d) 1
Correct Answer: b) 2
Explanation:
When i = 0, count becomes 0. When i = 1, continue skips the addition. When i = 2, count becomes 0 + 2 = 2.

7. What happens when executing the following code? outer: for (let i = 0; i < 2; i++) { inner: for (let j = 0; j < 2; j++) { if (i === 0 && j === 1) break outer; console.log(`${i}${j}`); } }

a) 00
b) 00 01
c) 00 01 10 11
d) 00 10
Correct Answer: a) 00
Explanation:
When i = 0, j = 0 logs '00'. Next, for j = 1, the condition (i === 0 && j === 1) is true, executing 'break outer', which immediately terminates the outer loop.

8. What is the result of using an un-initialized condition in a standard for loop `for (let i = 0; ; i++)`?

a) SyntaxError
b) The loop never runs
c) The condition defaults to true, resulting in an infinite loop unless broken internally
d) The loop runs exactly once
Correct Answer: c) The condition defaults to true, resulting in an infinite loop unless broken internally
Explanation:
In JavaScript for loops, an omitted condition section defaults to evaluating as true, creating an infinite loop.

9. What is the output of the following code? const arr = [10, 20, 30]; let res = ""; for (const index in arr) { res += typeof index + " "; } console.log(res.trim());

a) number number number
b) string string string
c) object object object
d) undefined undefined undefined
Correct Answer: b) string string string
Explanation:
The for...in loop yields key names as strings, even when iterating over array index positions.

10. What is the output of the following code? const arr = [10, 20, 30]; let res = ""; for (const value of arr) { res += typeof value + " "; } console.log(res.trim());

a) number number number
b) string string string
c) object object object
d) undefined undefined undefined
Correct Answer: a) number number number
Explanation:
The for...of loop yields array values directly, which are primitive numbers in this array.

11. What will be printed to the console? let val = 0; for (var i = 0; i < 3; i++) { val += i; } console.log(i);

a) 2
b) 3
c) ReferenceError
d) undefined
Correct Answer: b) 3
Explanation:
Variables declared with 'var' are function-scoped or globally scoped, so 'i' remains accessible after the loop ends with the value 3.

12. What will be printed to the console? let val = 0; for (let i = 0; i < 3; i++) { val += i; } console.log(i);

a) 2
b) 3
c) ReferenceError
d) undefined
Correct Answer: c) ReferenceError
Explanation:
Variables declared with 'let' inside a for loop statement are block-scoped to the loop, causing a ReferenceError if accessed outside.

13. What construct is used to iterate over asynchronous iterables in JavaScript?

a) for async...of
b) for await...of
c) while await
d) async for...in
Correct Answer: b) for await...of
Explanation:
The 'for await...of' statement allows creating a loop iterating over asynchronous iterable objects sequentially.

14. What is the output of this script? let count = 0; let i = 0; do { count++; i++; } while (i < 0); console.log(count);

a) 0
b) 1
c) 2
d) Infinite loop
Correct Answer: b) 1
Explanation:
The do...while loop body runs once unconditionally before checking (1 < 0), which is false, terminating the loop with count = 1.

15. What will be printed by the following code snippet? const map = new Map([["a", 1], ["b", 2]]); let out = ""; for (const [key, val] of map) { out += `${key}:${val} `; } console.log(out.trim());

a) a:1 b:2
b) a b
c) 1 2
d) a,1 b,2
Correct Answer: a) a:1 b:2
Explanation:
Iterating over a Map using for...of yields key-value pair arrays, which are destructured into `key` and `val`.

16. Which statement about iterating over plain objects with for...of is TRUE?

a) Plain objects are directly iterable with for...of out of the box.
b) Plain objects throw a TypeError if used directly with for...of because they lack an @@iterator method.
c) for...of iterates over plain object key names.
d) for...of automatically converts object keys into array indexes.
Correct Answer: b) Plain objects throw a TypeError if used directly with for...of because they lack an @@iterator method.
Explanation:
Plain objects are not built-in iterables. Using `for (const x of {})` throws a `TypeError: {} is not iterable`.

17. What helper method can be combined with for...of to iterate over plain object entries?

a) Object.entries(obj)
b) Object.toIterable(obj)
c) Object.map(obj)
d) Object.valuesArray(obj)
Correct Answer: a) Object.entries(obj)
Explanation:
Object.entries(obj) returns an array of [key, value] pairs, which is an iterable structure compatible with for...of.

18. What is the output of the following nested loop statement? let result = 0; for (let i = 1; i <= 2; i++) { for (let j = 1; j <= 3; j++) { if (j === 2) break; result += 1; } } console.log(result);

a) 2
b) 6
c) 4
d) 3
Correct Answer: a) 2
Explanation:
For each of the 2 outer iterations, the inner loop runs j=1 (adds 1 to result) and then j=2 hits 'break', exiting the inner loop. Total = 1 + 1 = 2.

19. What is the value of 'i' after this loop completes? let i; for (i = 0; i < 5; i++) { if (i === 3) break; } console.log(i);

a) 3
b) 4
c) 5
d) 2
Correct Answer: a) 3
Explanation:
When i reaches 3, the condition (i === 3) evaluates to true and executes 'break', terminating the loop while i is equal to 3.

20. What is the output of the following loop? let i = 0; while (i < 3) { i++; if (i === 2) continue; console.log(i); }

a) 1 3
b) 1 2 3
c) 0 1 3
d) 2 3
Correct Answer: a) 1 3
Explanation:
i increments to 1 (logged). Then i increments to 2; (i === 2) is true, so continue skips logging. Then i increments to 3 (logged). Loop terminates.

21. What will be printed to the console? const str = "hello"; let count = 0; for (const char of str) { if (char === "l") count++; } console.log(count);

a) 1
b) 2
c) 0
d) 5
Correct Answer: b) 2
Explanation:
Strings are iterable objects. The for...of loop inspects each character in "hello" and increments count for the two 'l' characters.

22. What happens if you attempt to assign to a loop variable declared with 'const' inside a for...in loop body?

a) It throws a TypeError during that iteration.
b) It reassigns the object key globally.
c) It is silently ignored.
d) It causes a SyntaxError.
Correct Answer: a) It throws a TypeError during that iteration.
Explanation:
Declaring `for (const key in obj)` creates a new `const` binding for each iteration. Reassigning `key` inside the loop body throws a TypeError.

23. Which property inclusion behavior distinguishes for...in loops when iterating over objects?

a) It iterates only over own non-enumerable properties.
b) It iterates over own and inherited enumerable string properties.
c) It iterates only over Symbol properties.
d) It iterates over array elements in guaranteed numerical index order.
Correct Answer: b) It iterates over own and inherited enumerable string properties.
Explanation:
The for...in loop traverses all enumerable string properties of an object, including those inherited along its prototype chain.

24. How can you ensure a for...in loop only processes an object's own properties, ignoring prototype properties?

a) By using Object.hasOwn(obj, prop) or obj.hasOwnProperty(prop)
b) By using the 'own' keyword in the loop header
c) By using a while loop instead
d) for...in automatically ignores prototype properties
Correct Answer: a) By using Object.hasOwn(obj, prop) or obj.hasOwnProperty(prop)
Explanation:
Filtering with `Object.hasOwn(obj, prop)` inside the loop body screens out inherited properties from prototype chains.

25. What is the output of the following script? let out = ""; let i = 3; while (i--) { out += i + " "; } console.log(out.trim());

a) 3 2 1
b) 2 1 0
c) 3 2 1 0
d) 2 1
Correct Answer: b) 2 1 0
Explanation:
Condition check returns 3 (truthy), then i becomes 2 (logged). Check returns 2, i becomes 1 (logged). Check returns 1, i becomes 0 (logged). Check returns 0 (falsy), loop ends.

26. What is the output of the following script? let out = ""; let i = 3; while (--i) { out += i + " "; } console.log(out.trim());

a) 3 2 1
b) 2 1 0
c) 2 1
d) 3 2
Correct Answer: c) 2 1
Explanation:
--i decrements i to 2 first (truthy, logged). Then decrements to 1 (truthy, logged). Next decrements to 0 (falsy, loop ends). Output: '2 1'.

27. What will be printed by this code? let sum = 0; for (let i = 0; i < 5; i++) { if (i % 2 !== 0) continue; sum += i; } console.log(sum);

a) 6
b) 4
c) 9
d) 10
Correct Answer: a) 6
Explanation:
The loop skips odd numbers and sums even numbers: i = 0, 2, 4. Total = 0 + 2 + 4 = 6.

28. What happens if 'continue' is called with a label for a block statement that is NOT a loop?

a) It breaks out of the labeled block.
b) It throws a SyntaxError.
c) It restarts the block execution.
d) It behaves like a break statement.
Correct Answer: b) It throws a SyntaxError.
Explanation:
Unlike 'break', which can target any labeled block, a labeled 'continue' statement can ONLY target a labeled loop statement.

29. What will be printed to the console? let msg = ""; block: { msg += "A"; break block; msg += "B"; } console.log(msg);

a) AB
b) A
c) SyntaxError
d) undefined
Correct Answer: b) A
Explanation:
A labeled 'break' statement can be used with arbitrary block statements to exit the block early.

30. What will be printed to the console? let out = 0; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === j) continue; out++; } } console.log(out);

a) 9
b) 6
c) 3
d) 0
Correct Answer: b) 6
Explanation:
The total combinations of (i, j) for 3x3 is 9. The condition (i === j) matches 3 times (0,0; 1,1; 2,2) and skips them, leaving 9 - 3 = 6.

31. What statement allows defining custom iteration behavior for custom objects used with for...of?

a) Symbol.iterator
b) Symbol.asyncIterator
c) Object.prototype.loop
d) Array.from
Correct Answer: a) Symbol.iterator
Explanation:
An object becomes iterable for 'for...of' when it implements a method whose key is Symbol.iterator.

32. What will be the output of this script? let str = ""; const arr = ["a", "b", "c"]; for (let i = 0; i < arr.length; i++) { if (i === 1) break; str += arr[i]; } console.log(str);

a) a
b) ab
c) abc
d) b
Correct Answer: a) a
Explanation:
At i = 0, str becomes 'a'. At i = 1, break executes immediately before appending 'b', terminating the loop.

33. What is the result of evaluating this loop? let i = 10; while (i < 10) { console.log("Run"); i++; }

a) Prints 'Run' once
b) Prints 'Run' 10 times
c) No output
d) Infinite loop
Correct Answer: c) No output
Explanation:
Condition (10 < 10) is false initially, so the while loop body is skipped completely.

34. What is the output of the following program? let count = 0; for (let i = 0; i < 2; i++) { let j = 0; while (j < 2) { count++; j++; } } console.log(count);

a) 2
b) 4
c) 6
d) 8
Correct Answer: b) 4
Explanation:
The outer for loop runs 2 times. For each outer pass, the inner while loop runs 2 times, yielding 2 * 2 = 4 counts.

35. Which loop statement allows executing iterations asynchronously over a Generator returning Promises?

a) for await...of
b) for...of
c) forEach
d) while async
Correct Answer: a) for await...of
Explanation:
The 'for await...of' statement awaits promises yielded by an asynchronous iterable or generator during iteration.

36. What output is produced by the following code? let val = ""; const obj = { x: 1, y: 2 }; for (const key in obj) { val += obj[key]; } console.log(val);

a) xy
b) 12
c) x1y2
d) undefined
Correct Answer: b) 12
Explanation:
The loop retrieves property keys ('x', 'y') and accesses object property values via `obj[key]`, concatenating '1' and '2' to get '12'.

37. What will be printed to the console? let i = 0; for (; i < 3; ) { i++; } console.log(i);

a) 2
b) 3
c) 4
d) SyntaxError
Correct Answer: b) 3
Explanation:
Omitting initialization and step expressions in a for loop header is valid syntax. i increments from 0 to 3, ending the loop.

38. What will be printed to the console? for (let i = 0; i < 3; i++) {} console.log(typeof i);

a) number
b) undefined
c) string
d) object
Correct Answer: b) undefined
Explanation:
Wait, 'i' is block-scoped to the loop. Accessing 'i' outside throws a ReferenceError, but `typeof i` safely evaluates to "undefined" for undeclared block variables.

39. What will be logged by the code snippet below? let total = 0; for (let i = 1; i <= 4; i++) { if (i === 3) break; total += i; } console.log(total);

a) 3
b) 6
c) 10
d) 1
Correct Answer: a) 3
Explanation:
i = 1 adds 1. i = 2 adds 2 (total = 3). i = 3 hits 'break' and exits immediately. Final total = 3.

40. What happens when using a 'return' statement inside a loop body within a function?

a) It breaks the loop and continues executing the remainder of the function.
b) It terminates both the loop and the entire function execution, returning the specified value.
c) It behaves identically to a 'continue' statement.
d) It causes a runtime error.
Correct Answer: b) It terminates both the loop and the entire function execution, returning the specified value.
Explanation:
A 'return' statement inside a loop ends function execution immediately, returning control to the caller.

41. What is the output of the following function execution? function testLoop() { for (let i = 0; i < 5; i++) { if (i === 2) return i; } } console.log(testLoop());

a) 2
b) 5
c) 0 1 2
d) undefined
Correct Answer: a) 2
Explanation:
When i reaches 2, the function immediately returns 2, ending both loop and function execution.

42. What will be logged by the following code? let str = ""; let i = 0; do { str += i; i++; } while (i < 3); console.log(str);

a) 012
b) 0123
c) 123
d) 0
Correct Answer: a) 012
Explanation:
Iterations append i: 0 (i becomes 1), 1 (i becomes 2), 2 (i becomes 3). Condition (3 < 3) is false. Result is '012'.

43. What will be printed to the console? const arr = ["a", "b"]; for (const val of arr) { val = "c"; }

a) Array updated to ['c', 'c']
b) Throws TypeError
c) No error, array remains ['a', 'b']
d) Throws SyntaxError
Correct Answer: b) Throws TypeError
Explanation:
Attempting to reassign the loop variable 'val' declared with 'const' inside the loop body throws a TypeError.

44. What will be printed to the console? const arr = ["a", "b"]; for (let val of arr) { val = "c"; } console.log(arr);

a) ['c', 'c']
b) ['a', 'b']
c) undefined
d) TypeError
Correct Answer: b) ['a', 'b']
Explanation:
Reassigning the local loop variable 'val' only alters the primitive binding inside the iteration scope, not the original array elements.

45. What is the output of the following nested loop? let out = ""; for (let i = 0; i < 2; i++) { for (let j = 0; j < 2; j++) { out += `${i}${j} `; } } console.log(out.trim());

a) 00 01 10 11
b) 00 01 02
c) 00 11
d) 0 1 2 3
Correct Answer: a) 00 01 10 11
Explanation:
Outer loop i=0 runs inner loop j=0,1 ('00', '01'). Outer loop i=1 runs inner loop j=0,1 ('10', '11').

46. Which built-in object is NOT iterable with for...of?

a) Array
b) String
c) Plain Object ({})
d) Map
Correct Answer: c) Plain Object ({})
Explanation:
Standard plain JavaScript objects `{}` do not implement the iterator protocol and cannot be looped over using `for...of` directly.

47. What will be printed to the console? let count = 0; for (let i = 0; i < 10; i++) { if (i === 5) break; count++; } console.log(count);

a) 5
b) 6
c) 10
d) 4
Correct Answer: a) 5
Explanation:
count increments for i = 0, 1, 2, 3, 4 (count becomes 5). When i = 5, break executes before incrementing count.

48. What is the output of this script? let val = 0; for (let i = 0; i < 5; i++) { if (i < 3) continue; val += i; } console.log(val);

a) 7
b) 12
c) 3
d) 10
Correct Answer: a) 7
Explanation:
i = 0, 1, 2 are skipped by continue. For i = 3 and 4, val += i computes 3 + 4 = 7.

49. What will happen if a break statement is executed inside a catch block within a loop?

a) It exits the catch block only.
b) It exits the loop containing the try-catch block.
c) It causes a SyntaxError.
d) It restarts the try block.
Correct Answer: b) It exits the loop containing the try-catch block.
Explanation:
A break statement inside a catch block breaks out of the enclosing loop containing the try-catch structure.

50. What output is produced by this code snippet? let res = ""; let i = 0; while (i < 3) { try { if (i === 1) { i++; continue; } res += i; i++; } catch(e) {} } console.log(res);

a) 02
b) 012
c) 12
d) Infinite loop
Correct Answer: a) 02
Explanation:
When i = 0, res becomes '0'. When i = 1, i increments to 2 and continue skips appending. When i = 2, res becomes '02'. Loop ends.

51. What will be logged by the following code? let x = 0; for (let i = 0; i < 3; i++) { setTimeout(() => { x += i; }, 10); } setTimeout(() => console.log(x), 50);

a) 3
b) 6
c) 0
d) 9
Correct Answer: a) 3
Explanation:
Wait, let's trace: i=0 (add 0), i=1 (add 1), i=2 (add 2). Total x = 0 + 1 + 2 = 3.

52. What will be logged by the following code using var? let x = 0; for (var i = 0; i < 3; i++) { setTimeout(() => { x += i; }, 10); } setTimeout(() => console.log(x), 50);

a) 3
b) 9
c) 6
d) 0
Correct Answer: b) 9
Explanation:
Since 'var' creates a single shared variable 'i' that equals 3 after loop completion, all 3 timeout callbacks add 3 to x, yielding 3 + 3 + 3 = 9.

53. Which statement best describes the infinite loop `for (;;)`?

a) It causes a parse-time SyntaxError.
b) It is valid JavaScript syntax creating a loop that runs indefinitely until broken.
c) It executes exactly 0 times.
d) It automatically breaks after 1000 iterations.
Correct Answer: b) It is valid JavaScript syntax creating a loop that runs indefinitely until broken.
Explanation:
All three expressions in a standard for loop are optional. `for (;;)` is valid syntax creating an infinite loop.

54. What is the result of executing the following script? let out = ""; for (let char of "123") { out = char + out; } console.log(out);

a) 123
b) 321
c) 3
d) 1
Correct Answer: b) 321
Explanation:
Prepending each character (`out = char + out`) reverses the order of elements from "123" to "321".

55. What will be printed to the console? const arr = [1, 2, 3]; let count = 0; for (let i = 0; i < arr.length; i++) { arr.push(4); count++; if (count === 3) break; } console.log(count);

a) 3
b) Infinite loop without condition
c) 1
d) TypeError
Correct Answer: a) 3
Explanation:
Modifying the array length during iteration would normally extend the loop, but the explicit `count === 3` condition breaks the loop at 3 iterations.

56. What output is generated by the following script? let a = 0; for (let i = 0; i < 3; i++) { switch (i) { case 1: a += 10; break; default: a += 1; } } console.log(a);

a) 12
b) 3
c) 30
d) 10
Correct Answer: a) 12
Explanation:
When i = 0: default runs (a = 1). When i = 1: case 1 runs (a = 1 + 10 = 11). When i = 2: default runs (a = 11 + 1 = 12).

57. Does a 'break' statement inside a switch block contained in a loop exit the loop?

a) Yes, it exits both the switch block and the outer loop.
b) No, it exits only the switch block, allowing the loop to continue.
c) It causes a SyntaxError.
d) It exits the enclosing function.
Correct Answer: b) No, it exits only the switch block, allowing the loop to continue.
Explanation:
An unlabelled 'break' inside a switch block terminates only the switch statement, not the surrounding loop.

58. How can you break out of an outer loop directly from inside a nested switch block?

a) Using a labeled break statement targeting the outer loop label
b) Using 'break break;'
c) Using 'continue loop;'
d) It is impossible in JavaScript
Correct Answer: a) Using a labeled break statement targeting the outer loop label
Explanation:
Prefixing the outer loop with a label (e.g., `outerLoop: for(...)`) allows `break outerLoop;` from within an inner switch block.

59. What is the output of the following code snippet? let res = 0; loop1: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1) break loop1; res++; } } console.log(res);

a) 3
b) 6
c) 9
d) 1
Correct Answer: a) 3
Explanation:
When i = 0, inner loop runs 3 times (res = 3). When i = 1, `break loop1` executes immediately, terminating the outer loop.

60. What will be printed to the console? let res = 0; loop1: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (j === 1) continue loop1; res++; } } console.log(res);

a) 3
b) 6
c) 9
d) 1
Correct Answer: a) 3
Explanation:
For each outer iteration (i = 0, 1, 2), j = 0 increments res (1 per outer pass), then j = 1 hits `continue loop1`, skipping j = 2 and starting the next outer iteration. Total res = 3.

61. What is the output of iterating over a string using for...in?

a) The string characters
b) The numeric index positions as strings ("0", "1", ...)
c) The length of the string
d) TypeError
Correct Answer: b) The numeric index positions as strings ("0", "1", ...)
Explanation:
for...in iterates over property keys of an object. For strings, key names are index character strings ("0", "1", etc.).

62. What is the output of iterating over a string using for...of?

a) The string characters
b) The numeric index positions as numbers
c) The character codes
d) TypeError
Correct Answer: a) The string characters
Explanation:
for...of iterates over values of iterable objects. For strings, it yields individual string characters.

63. What will be logged by the following code? let x = 0; for (let i = 0; i < 3; i++) { x += i; } console.log(x);

a) 3
b) 6
c) 0
d) 2
Correct Answer: a) 3
Explanation:
x accumulates 0 + 1 + 2 = 3.

64. What will be printed to the console? let arr = [10, 20]; arr.customProp = "hello"; let out = ""; for (let key in arr) { out += key + " "; } console.log(out.trim());

a) 0 1
b) 10 20
c) 0 1 customProp
d) 10 20 hello
Correct Answer: c) 0 1 customProp
Explanation:
for...in iterates over ALL enumerable string properties of an object, including custom added properties on array instances.

65. What will be printed to the console? let arr = [10, 20]; arr.customProp = "hello"; let out = ""; for (let val of arr) { out += val + " "; } console.log(out.trim());

a) 0 1
b) 10 20
c) 0 1 customProp
d) 10 20 hello
Correct Answer: b) 10 20
Explanation:
for...of uses the array's built-in iterator, which iterates strictly over indexed array elements [10, 20], ignoring non-indexed properties.

66. What will be the output of this code snippet? let count = 0; for (let i = 0; i < 5; i++) { if (i == "3") break; count++; } console.log(count);

a) 3
b) 5
c) 4
d) 0
Correct Answer: a) 3
Explanation:
Loose equality (i == "3") performs type coercion. When i = 3, 3 == "3" is true, triggering break and leaving count = 3.

67. What will be the output of this code snippet? let count = 0; for (let i = 0; i < 5; i++) { if (i === "3") break; count++; } console.log(count);

a) 3
b) 5
c) 4
d) 0
Correct Answer: b) 5
Explanation:
Strict equality (i === "3") checks types without coercion. Number i never strictly equals string "3", so break never triggers and count becomes 5.

68. Which statement accurately describes code execution during a do...while loop when the condition is initially false?

a) The loop body is skipped entirely.
b) The loop body executes exactly once.
c) The loop causes a runtime exception.
d) The loop enters an infinite loop state.
Correct Answer: b) The loop body executes exactly once.
Explanation:
The condition check occurs at the end of the block in a do...while loop, guaranteeing one execution pass regardless of condition state.

69. What is the output of the following loop? let out = ""; for (let i = 3; i > 0; i--) { out += i; } console.log(out);

a) 123
b) 321
c) 3210
d) 210
Correct Answer: b) 321
Explanation:
i starts at 3 and decrements through 3, 2, 1. When i reaches 0, condition i > 0 fails. Output is '321'.

70. What is the result of executing the following script? let a = 0; for (let i = 0; i < 3; i++) { if (i === 0) continue; a += i; } console.log(a);

a) 3
b) 0
c) 2
d) 1
Correct Answer: a) 3
Explanation:
i = 0 is skipped by continue. i = 1 adds 1. i = 2 adds 2. Total a = 1 + 2 = 3.

71. What is the evaluation of `for (let i = 0; i < 3; ++i)` compared to `for (let i = 0; i < 3; i++)`?

a) ++i executes fewer iterations than i++.
b) ++i increments i before the first iteration body runs.
c) Both forms produce identical loop behavior and iteration count.
d) i++ produces a SyntaxError in modern strict mode.
Correct Answer: c) Both forms produce identical loop behavior and iteration count.
Explanation:
The step expression in a for loop runs at the end of each iteration. Whether prefix (++i) or postfix (i++) is used, the increment value is discarded and produces identical loop outcomes.

72. What will be printed to the console? let count = 0; let obj = { a: 1, b: 2, c: 3 }; for (let prop in obj) { count++; } console.log(count);

a) 3
b) 6
c) 0
d) undefined
Correct Answer: a) 3
Explanation:
The object has 3 enumerable properties ('a', 'b', 'c'). The for...in loop runs 3 times.

73. What will be printed to the console? let count = 0; let obj = Object.create(null); obj.a = 1; for (let prop in obj) { count++; } console.log(count);

a) 1
b) 0
c) TypeError
d) undefined
Correct Answer: a) 1
Explanation:
Object.create(null) creates an object with no prototype. Adding property 'a' gives 1 enumerable key, so for...in runs 1 time.

74. What is the output of the following script? let str = ""; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === j) { str += "*"; } } } console.log(str);

a) ***
b) *********
c) *
d) ******
Correct Answer: a) ***
Explanation:
The condition i === j matches 3 times across the nested grid (0,0; 1,1; 2,2), appending 3 asterisks.

75. What is the outcome of using a break statement with an non-existent label `break missingLabel;`?

a) SyntaxError: Undefined label 'missingLabel'
b) It silently ignores the break.
c) It exits the global script.
d) It causes an unhandled ReferenceError at runtime.
Correct Answer: a) SyntaxError: Undefined label 'missingLabel'
Explanation:
Referencing an undefined label in a break or continue statement raises a parse-time SyntaxError.

76. What is the output of the following code? let val = 10; while (val > 0) { val -= 3; } console.log(val);

a) -2
b) -1
c) 0
d) 1
Correct Answer: a) -2
Explanation:
Values of val: 10 -> 7 -> 4 -> 1 -> -2. At -2, condition (-2 > 0) evaluates to false, leaving val as -2.

77. What will be printed to the console? let sum = 0; for (let i = 1; i <= 3; i++) { sum += i * i; } console.log(sum);

a) 14
b) 9
c) 6
d) 36
Correct Answer: a) 14
Explanation:
Calculates sum of squares: 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14.

78. Which loop control keyword is used inside generator functions to pause execution flow?

a) yield
b) return
c) await
d) break
Correct Answer: a) yield
Explanation:
The 'yield' keyword pauses generator function execution and returns a value to the caller.

79. What happens if you iterate over an empty array using for...of?

a) The loop body never executes.
b) It executes once with undefined.
c) It throws a TypeError.
d) It returns null.
Correct Answer: a) The loop body never executes.
Explanation:
Iterating over an empty iterable produces 0 iterations, skipping the loop body completely.

80. What happens if you iterate over an empty array using for...in?

a) The loop body never executes.
b) It executes once with index 0.
c) It throws a TypeError.
d) It returns null.
Correct Answer: a) The loop body never executes.
Explanation:
An empty array has no enumerable property keys, so for...in skips the loop body.

81. What will be printed to the console? let a = 0; for (let i = 0; i < 3; i++) { a += i; i++; } console.log(a);

a) 2
b) 3
c) 6
d) 0
Correct Answer: a) 2
Explanation:
Iteration 1: i=0, a += 0 (a=0), i++ makes i=1. Loop step i++ makes i=2. Iteration 2: i=2, a += 2 (a=2), i++ makes i=3. Loop step i++ makes i=4. Loop ends. a = 2.

82. What will be printed to the console? let str = ""; for (let i = 0; i < 2; i++) { str += "A"; for (let j = 0; j < 2; j++) { str += "B"; break; } } console.log(str);

a) ABAB
b) ABBABB
c) AB
d) AABB
Correct Answer: a) ABAB
Explanation:
Outer loop i=0: appends 'A'. Inner loop j=0: appends 'B' and breaks inner loop. Outer loop i=1: appends 'A'. Inner loop j=0: appends 'B' and breaks. Result: 'ABAB'.

83. What is the output of the following script? let res = 0; for (let i = 0; i < 3; i++) { if (i === 1) continue; for (let j = 0; j < 2; j++) { res++; } } console.log(res);

a) 4
b) 6
c) 2
d) 0
Correct Answer: a) 4
Explanation:
Outer loop runs for i=0 (inner runs 2 times, res=2), i=1 (skipped via continue), i=2 (inner runs 2 times, res=4).

84. What output is generated by the code snippet below? let x = 0; while (x < 5) { if (x === 3) break; x++; } console.log(x);

a) 3
b) 4
c) 5
d) 0
Correct Answer: a) 3
Explanation:
x increments through 1, 2, 3. When x reaches 3, condition (x === 3) triggers break, exiting with x = 3.

85. What is the difference between Array.prototype.forEach and for...of loop regarding 'async/await'?

a) for...of sequentially awaits promises in body, while forEach runs callbacks concurrently without awaiting.
b) forEach awaits promises automatically.
c) for...of cannot be used inside async functions.
d) There is no difference in async behavior.
Correct Answer: a) for...of sequentially awaits promises in body, while forEach runs callbacks concurrently without awaiting.
Explanation:
A for...of loop inside an async function properly pauses execution on `await`. Array.prototype.forEach invokes callback functions synchronously without awaiting promises.

86. What is the output of the following script? let sum = 0; const numbers = [10, 20, 30]; numbers.forEach(n => { sum += n; }); console.log(sum);

a) 60
b) 30
c) 0
d) undefined
Correct Answer: a) 60
Explanation:
Array.prototype.forEach executes the callback for each element in the array, accumulating 10 + 20 + 30 = 60.

87. What is the output of the following script? let out = ""; for (let i = 0; i < 3; i++) { try { if (i === 1) throw new Error(); out += i; } catch (e) { out += "E"; } } console.log(out);

a) 0E2
b) 012
c) 0E
d) E12
Correct Answer: a) 0E2
Explanation:
i=0 logs '0'. i=1 throws error, catch block appends 'E'. i=2 logs '2'. Final output: '0E2'.

88. What will be printed to the console? let out = ""; for (let i = 0; i < 3; i++) { try { if (i === 1) throw new Error(); out += i; } catch (e) { out += "E"; } finally { out += "F"; } } console.log(out);

a) 0F EF 2F
b) 0F E 2F
c) 0FEF2F
d) 0E2FFF
Correct Answer: c) 0FEF2F
Explanation:
i=0: appends '0' then 'F'. i=1: throws, catch appends 'E', finally appends 'F'. i=2: appends '2' then 'F'. Result: '0FEF2F'.

89. Which of the following describes the iteration scope of loop variables declared with `let` in a for loop?

a) A single scope shared across all loop iterations.
b) A new scope created for each iteration step.
c) Global window scope.
d) Function parameter scope.
Correct Answer: b) A new scope created for each iteration step.
Explanation:
JavaScript creates a new per-iteration environment record for `let` declarations in for loop headers, enabling closures inside loops to capture correct values.

90. What will be printed by this code? const funcs = []; for (let i = 0; i < 3; i++) { funcs.push(() => i); } console.log(funcs[0](), funcs[1](), funcs[2]());

a) 0 1 2
b) 3 3 3
c) 0 0 0
d) undefined undefined undefined
Correct Answer: a) 0 1 2
Explanation:
Because `let` creates a distinct variable binding per iteration, each closure captures its own iteration's value of `i`.

91. What will be printed by this code? const funcs = []; for (var i = 0; i < 3; i++) { funcs.push(() => i); } console.log(funcs[0](), funcs[1](), funcs[2]());

a) 0 1 2
b) 3 3 3
c) 0 0 0
d) undefined undefined undefined
Correct Answer: b) 3 3 3
Explanation:
Because `var` is function-scoped, all closures bind to the same single variable `i`, which equals 3 after loop completion.

92. What is the output of evaluating this statement? let out = ""; for (const char of "a\nb") { if (char === "\n") continue; out += char; } console.log(out);

a) ab
b) a\nb
c) a
d) b
Correct Answer: a) ab
Explanation:
Iterating over string "a\nb" yields 'a', '\n', and 'b'. The condition skips the newline character, concatenating to 'ab'.

93. What output is produced by the following snippet? let count = 0; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i + j === 2) continue; count++; } } console.log(count);

a) 6
b) 9
c) 3
d) 7
Correct Answer: a) 6
Explanation:
Total grid points = 9. The condition i + j === 2 matches for (0,2), (1,1), and (2,0), skipping 3 times. Remaining count = 9 - 3 = 6.

94. What is the output of the following program? let val = 1; while (val < 10) { val *= 2; } console.log(val);

a) 16
b) 8
c) 10
d) 32
Correct Answer: a) 16
Explanation:
val sequence: 1 -> 2 -> 4 -> 8 -> 16. At 16, condition (16 < 10) is false, terminating loop with val = 16.

95. What will be printed to the console? let val = 1; do { val *= 2; } while (val < 0); console.log(val);

a) 2
b) 1
c) 0
d) Infinite loop
Correct Answer: a) 2
Explanation:
The do...while loop body runs once, setting val to 2. Condition (2 < 0) is false, terminating loop.

96. Which statement about break and continue statements is TRUE?

a) break can be used in loops and switch statements; continue can only be used in loops.
b) Both break and continue can be used freely anywhere in JavaScript code.
c) continue can be used inside switch statements without loops.
d) break cannot be used with labeled statements.
Correct Answer: a) break can be used in loops and switch statements; continue can only be used in loops.
Explanation:
The break statement applies to loops, switch statements, and labeled blocks. The continue statement applies exclusively to loops.

97. What will be printed to the console? let str = ""; for (let i = 0; i < 3; i++) { str += i; if (i === 1) i = 5; } console.log(str);

a) 01
b) 015
c) 012
d) 012345
Correct Answer: a) 01
Explanation:
i=0 appends '0'. i=1 appends '1' and reassigns i to 5. The loop step i++ increments i to 6. Condition (6 < 3) is false, ending loop. Result: '01'.

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

a) 0
b) 3
c) 1
d) Infinite loop
Correct Answer: a) 0
Explanation:
Iteration 1: i=0, sum becomes 0, i reassigned to 2. Loop step i++ makes i=3. Condition (3 < 3) is false, ending loop. Final sum = 0.

99. What will be logged by the code snippet below? let count = 0; const text = "abc"; for (let i = 0; i < text.length; i++) { count++; } console.log(count);

a) 3
b) 2
c) 0
d) undefined
Correct Answer: a) 3
Explanation:
text.length is 3. The loop runs for i = 0, 1, 2, incrementing count 3 times.

100. What is the result of executing this code? let out = ""; for (const val of [1, 2, 3]) { if (val === 2) continue; out += val; } console.log(out);

a) 13
b) 123
c) 23
d) 12
Correct Answer: a) 13
Explanation:
When val === 2, continue skips appending '2'. Output is '13'.

101. Which of the following describes the execution order of expressions in a standard `for (expr1; expr2; expr3)` loop?

a) expr1 runs once before loop; expr2 evaluated before each iteration; expr3 evaluated after each iteration body.
b) expr1, expr2, expr3 all run before every iteration.
c) expr1 runs before each iteration; expr2 runs after; expr3 runs on termination.
d) expr3 runs first, then expr2, then expr1.
Correct Answer: a) expr1 runs once before loop; expr2 evaluated before each iteration; expr3 evaluated after each iteration body.
Explanation:
Initialization (expr1) executes once. Condition (expr2) is tested before each pass. Increment/step (expr3) evaluates after each pass.

102. What will be printed to the console? let sum = 0; for (let i = 5; i > 0; i -= 2) { sum += i; } console.log(sum);

a) 9
b) 15
c) 8
d) 5
Correct Answer: a) 9
Explanation:
i sequence: 5, 3, 1. (Next step is -1, which fails condition > 0). Sum = 5 + 3 + 1 = 9.

103. What is the output of the following code snippet? let result = ""; const str = "hello"; for (let i = str.length - 1; i >= 0; i--) { result += str[i]; } console.log(result);

a) olleh
b) hello
c) o
d) h
Correct Answer: a) olleh
Explanation:
The loop iterates backward through the string characters from last index to first index, reversing 'hello' to 'olleh'.
← Previous: JavaScript Interview Questions MCQs
Next →: JavaScript OOP MCQs for Developer Interviews & Certification
Java Functions MCQs

Java Functions MCQs

In Java, functions are formally known as methods and serve as modular blocks of reusable code encapsulated within classes. A…

By MCQs Generator
JavaScript Control Flow MCQs

JavaScript Control Flow MCQs

Control flow defines the order in which statements and expressions execute within a JavaScript program. At the heart of decision-making…

By MCQs Generator
JavaScript Error Handling MCQs for Developer Interviews & Certification

JavaScript Error Handling MCQs for Developer Interviews & Certification

Exception and error handling in JavaScript is essential for preventing runtime crashes and maintaining application stability across complex web environments.…

By MCQs Generator