JavaScript Control Flow MCQs

1 min read

Control flow defines the order in which statements and expressions execute within a JavaScript program. At the heart of decision-making in JS are conditional statements: if, else if, and else blocks. These constructs evaluate boolean expressions or values implicitly coerced to booleans (truthy vs. falsy) to direct execution paths dynamically. Modern JavaScript development relies on mastering not only standard block-based branching but also shorthand alternatives like ternary expressions (? :) and short-circuit evaluation via logical operators (&&, ||). Understanding block scoping (let/const vs. var inside conditional blocks) and type coercion during evaluation is essential for writing error-free asynchronous and synchronous code

1. What is the output of the following code? let a = [1, 2, 3]; let b = [1, 2, 3]; if (a == b) { console.log("Equal"); } else { console.log("Not Equal"); }

a) Equal
b) Not Equal
c) TypeError
d) undefined
Correct Answer: b) Not Equal
Explanation:
In JavaScript, objects and arrays are compared by reference, not by value. Since 'a' and 'b' reference different memory addresses, a == b evaluates to false.

2. What will be printed to the console? let x = 10; let y = eval("x + 5"); console.log(y);

a) 15
b) 105
c) undefined
d) SyntaxError
Correct Answer: a) 15
Explanation:
eval() executes string code in the local environment context, evaluating the expression 'x + 5' (10 + 5) to 15.

3. What will be the output of the following code snippet? let count = 0; outer: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1 && j === 1) continue outer; count++; } } console.log(count);

a) 7
b) 8
c) 9
d) 4
Correct Answer: a) 7
Explanation:
When i=0, j runs 3 times (count=3). When i=1, j=0 increments count (4); j=1 triggers continue outer, skipping j=1 and j=2. When i=2, j runs 3 times (count=7).

4. What is the result of evaluating: Boolean(new Boolean(false))?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
new Boolean(false) constructs a wrapper object. All non-null objects in JavaScript evaluate to truthy when passed to Boolean().

5. What happens when a 'return' statement with a value is placed inside a constructor function called with 'new'?

a) The returned value overrides the instance only if it is an object.
b) The returned value always overrides the created instance.
c) A SyntaxError is thrown.
d) The return statement is completely ignored.
Correct Answer: a) The returned value overrides the instance only if it is an object.
Explanation:
Constructors invoked with 'new' return the newly created object unless an explicit object is returned. If a primitive is returned, it is ignored.

6. What will be the output of the following code snippet? console.log(1 || 2 && 0);

a) 1
b) 2
c) 0
d) false
Correct Answer: a) 1
Explanation:
Logical AND (&&) has higher precedence than OR (||). 2 && 0 evaluates to 0. Then 1 || 0 evaluates to 1.

7. What is the output of the following code? function test() { try { throw new Error("Err"); } catch (x) { var x = 1; console.log(x); } console.log(x); } test();

a) 1 followed by 1
b) 1 followed by undefined
c) 1 followed by ReferenceError
d) undefined followed by undefined
Correct Answer: b) 1 followed by undefined
Explanation:
The catch block parameter 'x' shadows the outer 'var x'. Assigning x inside catch updates the catch-scoped parameter, leaving the outer function-scoped 'var x' as undefined.

8. What is the output of the following expression? console.log(typeof (function() { return 1; })());

a) number
b) function
c) undefined
d) object
Correct Answer: a) number
Explanation:
The self-invoking function returns 1. typeof 1 yields the string "number".

9. What will be logged to the console? let x = 5; if (x > 0) { x = x + 2; } else if (x > 3) { x = x + 4; } console.log(x);

a) 7
b) 11
c) 5
d) 9
Correct Answer: a) 7
Explanation:
The first condition (x > 0) is true, so x becomes 7. The remaining 'else if' block in the chain is skipped entirely.

10. What is the output of the following snippet? for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 100); }

a) 3 3 3
b) 0 1 2
c) 0 0 0
d) 1 2 3
Correct Answer: a) 3 3 3
Explanation:
Because 'var' is function-scoped, all callbacks reference the single global loop variable 'i', which equals 3 after loop completion.

11. What will be the output of the following code? console.log(0 ?? "default");

a) 0
b) default
c) null
d) undefined
Correct Answer: a) 0
Explanation:
The nullish coalescing operator (??) falls back to the right operand only if the left operand is null or undefined. 0 is neither.

12. What will be the output of the following code? console.log(0 || "default");

a) 0
b) default
c) false
d) undefined
Correct Answer: b) default
Explanation:
The logical OR operator (||) falls back to the right operand if the left operand is falsy. 0 is falsy, so "default" is returned.

13. Which of the following is NOT a falsy value in JavaScript?

a) 0
b) 0n
c) "false"
d) NaN
Correct Answer: c) "false"
Explanation:
Any non-empty string, including "false", evaluates to a truthy value in JavaScript.

14. What is the evaluation result of: `console.log([] == ![]);`?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
![] converts to false. The comparison [] == false coerces both sides to numbers: [] becomes 0 and false becomes 0, so 0 == 0 is true.

15. What is the evaluation result of: `console.log([] === ![]);`?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: b) false
Explanation:
Strict equality checking compares type and value without coercion. [] is an object reference while ![] is boolean false.

16. What is the output of the following code snippet? let a = 10; switch (true) { case a > 5: console.log("A"); break; case a > 8: console.log("B"); break; }

a) A
b) B
c) A and B
d) undefined
Correct Answer: a) A
Explanation:
The switch expression evaluates to true. The first case (a > 5) evaluates to true, triggering case 'A' and breaking immediately.

17. What will be logged by the following code? function foo() { return { name: "JS" }; } console.log(foo());

a) { name: 'JS' }
b) undefined
c) SyntaxError
d) null
Correct Answer: b) undefined
Explanation:
Automatic Semicolon Insertion (ASI) places a semicolon after 'return', causing the function to return undefined.

18. What is the outcome of running the following script? let res = 0; for (let i = 0; i < 5; i++) { if (i % 2 === 0) continue; res += i; } console.log(res);

a) 4
b) 6
c) 9
d) 10
Correct Answer: a) 4
Explanation:
The condition skipping even numbers leaves odd numbers i=1 and i=3. The accumulated total is 1 + 3 = 4.

19. What output is printed by the following ternary structure? let mark = 75; let grade = mark >= 90 ? "A" : mark >= 70 ? "B" : "C"; console.log(grade);

a) A
b) B
c) C
d) undefined
Correct Answer: b) B
Explanation:
mark >= 90 is false. Evaluation shifts to mark >= 70, which is true (75 >= 70), returning "B".

20. Which statement will execute within a try-catch-finally block if an unhandled error occurs in the catch block?

a) The try block restarts.
b) The finally block executes, then the unhandled error propagates.
c) The finally block is skipped.
d) Script execution continues normally.
Correct Answer: b) The finally block executes, then the unhandled error propagates.
Explanation:
The finally block always runs before control leaves the try-catch structure, even if an exception is thrown inside catch.

21. What is the value of 'x' after this logical operator expression evaluates? let x = 10; x &&= 20; console.log(x);

a) 10
b) 20
c) true
d) false
Correct Answer: b) 20
Explanation:
The logical AND assignment operator (x &&= 20) evaluates the left operand. Since 10 is truthy, x is assigned 20.

22. What is the value of 'x' after this logical operator expression evaluates? let x = 0; x &&= 20; console.log(x);

a) 0
b) 20
c) false
d) undefined
Correct Answer: a) 0
Explanation:
Because x is 0 (falsy), logical AND assignment short-circuits and leaves x unchanged as 0.

23. What will be printed to the console? let obj = { a: 1 }; console.log(obj?.b?.c ?? "Missing");

a) undefined
b) Missing
c) null
d) TypeError
Correct Answer: b) Missing
Explanation:
Optional chaining safely returns undefined for obj?.b?.c. The nullish coalescing operator converts undefined to "Missing".

24. What will be the output of the following code? let x = 1; while (x < 5) { x += 2; } console.log(x);

a) 3
b) 5
c) 7
d) 4
Correct Answer: b) 5
Explanation:
Iterations: start x=1 -> x becomes 3 -> x becomes 5. At x=5, condition (5 < 5) is false, ending loop with x = 5.

25. What happens if a label identifier named 'loop' is used with 'break loop;' outside of any labeled loop or statement?

a) It silently fails.
b) SyntaxError: Undefined label 'loop'
c) It exits the global execution scope.
d) It returns undefined.
Correct Answer: b) SyntaxError: Undefined label 'loop'
Explanation:
Using a labeled break or continue statement targeting an un-declared or out-of-scope label identifier throws a SyntaxError.

26. What is the output of the following statement? console.log(false || 0 || "" || NaN);

a) false
b) 0
c) ""
d) NaN
Correct Answer: d) NaN
Explanation:
When all operands in a logical OR chain are falsy, the expression evaluates to the last operand (NaN).

27. What is the output of the following statement? console.log(true && 1 && "Text" && [1]);

a) true
b) 1
c) Text
d) [1]
Correct Answer: d) [1]
Explanation:
When all operands in a logical AND chain are truthy, the expression evaluates to the final operand ([1]).

28. What will be printed to the console? let i = 0; do { i++; } while (i < 0); console.log(i);

a) 0
b) 1
c) undefined
d) Infinite loop
Correct Answer: b) 1
Explanation:
A do...while loop runs the body once before checking condition. i increments from 0 to 1, then (1 < 0) evaluates to false.

29. What statement allows stopping iteration inside an array `.forEach()` method?

a) break
b) continue
c) return
d) There is no built-in way to stop a standard forEach loop prematurely.
Correct Answer: d) There is no built-in way to stop a standard forEach loop prematurely.
Explanation:
The Array.prototype.forEach method cannot be short-circuited with break or continue. To break early, use a standard for loop or for...of.

30. What will be logged by the code below? let val = "b"; switch(val) { case "a": console.log(1); case "b": console.log(2); case "c": console.log(3); break; default: console.log(4); }

a) 2
b) 2 3
c) 2 3 4
d) 1 2 3
Correct Answer: b) 2 3
Explanation:
Case "b" matches, printing 2. Because there is no break after case "b", execution falls through to case "c", printing 3 before breaking.

31. What statement is used to handle exceptions produced during execution of a 'try' block?

a) catch
b) throw
c) finally
d) reject
Correct Answer: a) catch
Explanation:
The 'catch' block contains error-handling statements executed if an error is thrown in the preceding try block.

32. What is the output of the code snippet below? let num = 10; if (num = 0) { console.log("True"); } else { console.log("False"); }

a) True
b) False
c) TypeError
d) ReferenceError
Correct Answer: b) False
Explanation:
The expression (num = 0) performs assignment, assigning 0 to num and evaluating to 0. Since 0 is falsy, the else block runs.

33. What will be printed to the console? let x = 1; if (true) { let x = 2; } console.log(x);

a) 1
b) 2
c) undefined
d) ReferenceError
Correct Answer: a) 1
Explanation:
'let' is block-scoped. The declaration inside the 'if' block is distinct from the outer variable 'x', which remains equal to 1.

34. What is the evaluation of: Boolean("0")?

a) false
b) true
c) 0
d) TypeError
Correct Answer: b) true
Explanation:
Non-empty string literals, including "0", evaluate to true when converted to a boolean.

35. What output is produced by the following snippet? let a = 10; let b = 20; if (a > 5 || b < 10) { console.log("Valid"); } else { console.log("Invalid"); }

a) Valid
b) Invalid
c) undefined
d) TypeError
Correct Answer: a) Valid
Explanation:
a > 5 is true (10 > 5). Due to short-circuiting in logical OR, the whole condition evaluates to true without checking b.

36. What will be logged by the code below? let i = 0; while (i < 3) { if (i === 1) { i++; continue; } console.log(i); i++; }

a) 0 2
b) 0 1 2
c) 1 2
d) 0 1
Correct Answer: a) 0 2
Explanation:
When i=0, 0 is printed. When i=1, i increments to 2 and continue skips log. When i=2, 2 is printed. Loop ends when i becomes 3.

37. What statement allows creating asynchronous iterator loops over async iterable objects?

a) for await...of
b) for async...in
c) while await
d) do await...while
Correct Answer: a) for await...of
Explanation:
The 'for await...of' construct iterates sequentially over asynchronous iterable objects using promises.

38. What is the output of the following statement? console.log(10 > 5 > 0);

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
10 > 5 evaluates to true. Then true > 0 coerces true to 1, evaluating 1 > 0 which is true.

39. What is the output of the following statement? console.log(5 > 10 > 0);

a) true
b) false
c) TypeError
d) undefined
Correct Answer: b) false
Explanation:
5 > 10 evaluates to false. Then false > 0 coerces false to 0, evaluating 0 > 0 which is false.

40. What will be printed to the console? let str = "JavaScript"; let count = 0; for (let char of str) { if (char === "a") count++; } console.log(count);

a) 2
b) 1
c) 0
d) 3
Correct Answer: a) 2
Explanation:
The string "JavaScript" contains two lowercase 'a' characters, incrementing count twice.

41. What is the result of executing: `throw new TypeError("Invalid Type");` inside a try block with no catch block but a finally block?

a) The error is ignored.
b) The finally block executes, then the TypeError is thrown up the call stack.
c) A SyntaxError is raised.
d) The program halts before the finally block runs.
Correct Answer: b) The finally block executes, then the TypeError is thrown up the call stack.
Explanation:
The finally block always executes. Since there is no catch block to intercept the exception, it propagates up after finally completes.

42. What will be the output of the following script? let obj = { key: "value" }; console.log("key" in obj);

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
The 'in' operator checks whether a specified property exists within an object or its prototype chain, returning true.

43. What will be printed by the following code? let arr = [10, 20, 30]; console.log(1 in arr);

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
In an array, property keys are numeric indices. Index 1 exists in [10, 20, 30], so '1 in arr' returns true.

44. What will be printed by the following code? let arr = [10, 20, 30]; console.log(30 in arr);

a) true
b) false
c) undefined
d) TypeError
Correct Answer: b) false
Explanation:
The 'in' operator checks for property keys/indices, not values. Index 30 does not exist in an array of length 3.

45. What statement can be used inside a switch case to prevent fall-through execution into the next case?

a) break
b) continue
c) stop
d) exit
Correct Answer: a) break
Explanation:
The 'break' statement terminates the switch block and prevents execution from falling through to subsequent cases.

46. What is the evaluated output of: `Boolean(NaN)`?

a) false
b) true
c) NaN
d) TypeError
Correct Answer: a) false
Explanation:
NaN is defined as one of the falsy values in JavaScript.

47. What output is generated by this script? let a = 0; if (a) { console.log("First"); } else if (a === 0) { console.log("Second"); } else { console.log("Third"); }

a) First
b) Second
c) Third
d) No output
Correct Answer: b) Second
Explanation:
a=0 is falsy, so the first condition fails. The second condition (0 === 0) is true, outputting 'Second'.

48. Which of the following describes the purpose of optional catch binding introduced in ES2019?

a) To write catch blocks without specifying an error parameter e.g., catch { }.
b) To ignore errors completely without running catch blocks.
c) To bind 'this' automatically inside catch blocks.
d) To make try blocks optional.
Correct Answer: a) To write catch blocks without specifying an error parameter e.g., catch { }.
Explanation:
Optional catch binding lets developers omit the error variable parameter when error details are unused inside catch.

49. What will be printed to the console? let x = 1; let y = x++ + ++x; console.log(y);

a) 4
b) 3
c) 5
d) 2
Correct Answer: a) 4
Explanation:
x++ evaluates to 1 (x becomes 2). Then ++x increments x to 3 and evaluates to 3. 1 + 3 = 4.

50. What is the result of evaluating the following ternary statement? let age = 20; let canVote = age >= 18 ? true : false; console.log(canVote);

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
20 >= 18 is true, returning the truthy expression true.

51. What will be logged by the code below? let a = ""; let b = a || "Fallback"; console.log(b);

a) ""
b) Fallback
c) undefined
d) null
Correct Answer: b) Fallback
Explanation:
Empty string "" is falsy, so logical OR falls back to the right operand "Fallback".

52. What will be logged by the code below? let a = ""; let b = a ?? "Fallback"; console.log(b);

a) ""
b) Fallback
c) undefined
d) null
Correct Answer: a) ""
Explanation:
Empty string "" is not null or undefined, so nullish coalescing returns "".

53. What is the output of the following nested loop statement? let count = 0; for (let i = 0; i < 2; i++) { for (let j = 0; j < 2; j++) { count++; } } console.log(count);

a) 2
b) 4
c) 6
d) 8
Correct Answer: b) 4
Explanation:
Outer loop runs 2 times; inner loop runs 2 times per outer iteration. Total executions = 2 * 2 = 4.

54. What will happen if you attempt to use 'break' outside of a loop, switch, or labeled statement?

a) It silently fails.
b) Uncaught SyntaxError: Illegal break statement
c) It exits the application.
d) It returns null.
Correct Answer: b) Uncaught SyntaxError: Illegal break statement
Explanation:
Using 'break' outside an iteration statement, switch block, or labeled statement raises a SyntaxError.

55. What will happen if you attempt to use 'continue' outside of a loop structure?

a) It silently fails.
b) Uncaught SyntaxError: Illegal continue statement
c) It exits the function.
d) It does nothing.
Correct Answer: b) Uncaught SyntaxError: Illegal continue statement
Explanation:
The 'continue' statement can only be used within loop bodies. Using it elsewhere causes a SyntaxError.

56. What is the output of this code? let x = 5; if (x > 10) { console.log("A"); } else { console.log("B"); }

a) A
b) B
c) undefined
d) TypeError
Correct Answer: b) B
Explanation:
5 > 10 is false, executing the else block.

57. What is the output of the following statement? console.log(false ? "A" : true ? "B" : "C");

a) A
b) B
c) C
d) undefined
Correct Answer: b) B
Explanation:
The initial condition is false, skipping "A". The secondary condition is true, returning "B".

58. What is the output of the following statement? console.log(false ? "A" : false ? "B" : "C");

a) A
b) B
c) C
d) undefined
Correct Answer: c) C
Explanation:
Both conditions evaluate to false, resolving the nested ternary chain to "C".

59. What statement allows defining custom error messages in JavaScript?

a) throw
b) raise
c) catch
d) error
Correct Answer: a) throw
Explanation:
The throw statement raises custom user-defined exceptions during code execution.

60. What will be logged by the code snippet below? let a = 0; try { a += 1; throw "Err"; a += 2; } catch (e) { a += 4; } finally { a += 8; } console.log(a);

a) 13
b) 5
c) 15
d) 9
Correct Answer: a) 13
Explanation:
a becomes 1 in try, error jumps to catch (a += 4 -> 5), finally runs unconditionally (a += 8 -> 13).

61. What output is produced by the following script? let sum = 0; for (let i = 1; i <= 3; i++) { sum += i; } console.log(sum);

a) 3
b) 6
c) 5
d) 0
Correct Answer: b) 6
Explanation:
The loop calculates 1 + 2 + 3 = 6.

62. Which loop guarantees that its body executes at least once before testing the condition?

a) for
b) while
c) do...while
d) for...of
Correct Answer: c) do...while
Explanation:
The do...while loop tests the condition expression after executing the loop body.

63. What will be printed to the console? let x = null; console.log(x?.toString());

a) TypeError
b) undefined
c) null
d) ReferenceError
Correct Answer: b) undefined
Explanation:
Optional chaining (?.) evaluates to undefined without calling the method if the target object is null.

64. What will be printed to the console? let x = null; console.log(x.toString());

a) TypeError
b) undefined
c) null
d) ReferenceError
Correct Answer: a) TypeError
Explanation:
Invoking properties or methods directly on null using dot notation raises a TypeError.

65. What is the value of 'result' after execution? let x = 10; let result = x > 5 ? "High" : "Low"; console.log(result);

a) High
b) Low
c) true
d) false
Correct Answer: a) High
Explanation:
10 > 5 is true, returning "High".

66. What will be printed by the following statement? console.log(Boolean(function() {}));

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
Functions are reference object types in JavaScript and always convert to truthy values.

67. What will be printed by the following statement? console.log(Boolean(Symbol("foo")));

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
Symbols are primitive values that always evaluate to truthy.

68. What is the output of the following statement? console.log(!!0);

a) true
b) false
c) 0
d) undefined
Correct Answer: b) false
Explanation:
0 is falsy. !0 is true, and !!0 converts it back to boolean false.

69. What is the output of the following statement? console.log(!!1);

a) true
b) false
c) 1
d) undefined
Correct Answer: a) true
Explanation:
1 is truthy. Double negation (!!) explicitly converts truthy values to boolean true.

70. What is the result of evaluating: `undefined || "default"`?

a) undefined
b) default
c) null
d) false
Correct Answer: b) default
Explanation:
undefined is falsy, so logical OR evaluates and returns the right-hand value "default".

71. What is the result of evaluating: `null || "default"`?

a) null
b) default
c) undefined
d) false
Correct Answer: b) default
Explanation:
null is falsy, so logical OR returns "default".

72. What is the result of evaluating: `false || "default"`?

a) false
b) default
c) true
d) null
Correct Answer: b) default
Explanation:
false is falsy, causing logical OR to return "default".

73. What output is generated by the following script? let a = 5; if (a == "5") { console.log("Loose"); } else { console.log("Strict"); }

a) Loose
b) Strict
c) undefined
d) TypeError
Correct Answer: a) Loose
Explanation:
Loose equality (==) coerces string "5" to number 5, evaluating 5 == 5 as true.

74. What output is generated by the following script? let a = 5; if (a === "5") { console.log("Strict"); } else { console.log("Mismatch"); }

a) Strict
b) Mismatch
c) undefined
d) TypeError
Correct Answer: b) Mismatch
Explanation:
Strict equality (===) compares type and value without coercion. Number 5 does not equal string "5".

75. What is the main function of the 'continue' statement in a loop?

a) Terminates the loop entirely.
b) Skips the rest of the current iteration block and moves to the next pass.
c) Restarts loop initialization.
d) Exits the current function context.
Correct Answer: b) Skips the rest of the current iteration block and moves to the next pass.
Explanation:
The continue keyword halts statement execution within the current loop iteration and advances execution flow.

76. What will be printed to the console? let x = 0; while (x < 3) { console.log(x); x++; }

a) 0 1 2
b) 0 1 2 3
c) 1 2 3
d) 0
Correct Answer: a) 0 1 2
Explanation:
The loop prints values 0, 1, and 2. When x reaches 3, condition (3 < 3) fails and loop terminates.

77. Which logical operator short-circuits and assigns a value if the variable is currently nullish?

a) &&=
b) ||=
c) ??=
d) ==
Correct Answer: c) ??=
Explanation:
The nullish assignment operator (??=) evaluates its left operand and performs assignment only if it is null or undefined.

78. What is the value of 'x' after evaluation? let x = null; x ??= 50; console.log(x);

a) null
b) 50
c) undefined
d) 0
Correct Answer: b) 50
Explanation:
Because x is null, the nullish assignment operator assigns 50 to x.

79. What is the value of 'x' after evaluation? let x = 100; x ??= 50; console.log(x);

a) 100
b) 50
c) null
d) undefined
Correct Answer: a) 100
Explanation:
Because x is 100 (not nullish), the nullish assignment operator does not execute, leaving x as 100.

80. What happens if a switch case contains no 'break' statement?

a) Execution falls through to the next case.
b) A SyntaxError is thrown.
c) The switch block immediately terminates.
d) The default clause is automatically executed.
Correct Answer: a) Execution falls through to the next case.
Explanation:
Without a break statement, execution continues into the subsequent case block regardless of condition checks.

81. What is the evaluation output of: `console.log("Dog" && "Cat");`?

a) Dog
b) Cat
c) true
d) false
Correct Answer: b) Cat
Explanation:
Since "Dog" is non-empty and truthy, logical AND evaluates and returns the second operand "Cat".

82. What is the evaluation output of: `console.log("Dog" || "Cat");`?

a) Dog
b) Cat
c) true
d) false
Correct Answer: a) Dog
Explanation:
Logical OR short-circuits on the first truthy operand encountered and returns "Dog".

83. What output will be logged by the code snippet below? let a = 10; if (a > 5) console.log("X"); console.log("Y");

a) X
b) Y
c) X followed by Y
d) No output
Correct Answer: c) X followed by Y
Explanation:
Without curly braces {}, only `console.log("X")` is conditional. `console.log("Y")` runs unconditionally.

84. What output will be logged by the code snippet below? let a = 2; if (a > 5) console.log("X"); console.log("Y");

a) X
b) Y
c) X followed by Y
d) No output
Correct Answer: b) Y
Explanation:
Without braces, `console.log("X")` is skipped because (2 > 5) is false. `console.log("Y")` executes unconditionally.

85. What error occurs if an undeclared variable is evaluated directly in an if statement condition?

a) TypeError
b) ReferenceError
c) SyntaxError
d) RangeError
Correct Answer: b) ReferenceError
Explanation:
Accessing an identifier that has not been declared anywhere throws a ReferenceError.

86. What will be printed to the console? let obj = { a: 10 }; with (obj) { console.log(a); }

a) 10
b) undefined
c) ReferenceError
d) TypeError
Correct Answer: a) 10
Explanation:
The 'with' statement extends scope chain to search properties of the provided object ('a' resolves to obj.a = 10).

87. Why is the use of the 'with' statement strongly discouraged in modern JavaScript?

a) It causes memory leaks automatically.
b) It makes code scope ambiguous, slow to optimize, and is forbidden in strict mode.
c) It only works with numbers.
d) It breaks all loops inside it.
Correct Answer: b) It makes code scope ambiguous, slow to optimize, and is forbidden in strict mode.
Explanation:
The 'with' statement introduces scope ambiguity, impairs performance optimization, and throws a SyntaxError in strict mode.

88. What is the output of the following statement? console.log(false || 0 || undefined || "Hit");

a) false
b) 0
c) undefined
d) Hit
Correct Answer: d) Hit
Explanation:
Logical OR checks left-to-right and returns the first truthy operand, which is string "Hit".

89. What will be printed to the console? let x = 1; switch (x) { case 1: console.log("One"); break; default: console.log("Other"); }

a) One
b) Other
c) One and Other
d) undefined
Correct Answer: a) One
Explanation:
x matches case 1 directly, outputting "One" and exiting the switch block.

90. What is the output of the following loop? let out = ""; for (let i = 0; i < 3; i++) { if (i === 1) continue; out += i; } console.log(out);

a) 012
b) 02
c) 12
d) 01
Correct Answer: b) 02
Explanation:
When i=1, continue skips adding 1 to 'out'. The string output is '02'.

91. What will be logged by the code snippet below? let i = 0; for (; i < 2; i++) {} console.log(i);

a) 0
b) 1
c) 2
d) undefined
Correct Answer: c) 2
Explanation:
The loop body is empty. i increments from 0 to 1, then to 2. At i=2, (2 < 2) is false, ending loop with i = 2.

92. What will be logged by the code snippet below? let a = 10; let b = a > 5 ? a < 20 ? "Middle" : "High" : "Low"; console.log(b);

a) Middle
b) High
c) Low
d) undefined
Correct Answer: a) Middle
Explanation:
a > 5 (10 > 5) is true. Nested condition a < 20 (10 < 20) is also true, returning "Middle".

93. What output is generated by evaluating: `Boolean(Infinity)`?

a) true
b) false
c) Infinity
d) TypeError
Correct Answer: a) true
Explanation:
Infinity and -Infinity are non-zero numbers and evaluate as truthy in JavaScript.

94. What will be printed to the console? let res = ""; let obj = { x: 1, y: 2 }; for (let k in obj) { res += k; } console.log(res);

a) xy
b) 12
c) x1y2
d) undefined
Correct Answer: a) xy
Explanation:
The 'for...in' loop iterates over enumerable string property keys of an object, concatenating 'x' and 'y'.

95. What statement can be used inside generator functions to pause execution and output a value?

a) yield
b) return
c) break
d) pause
Correct Answer: a) yield
Explanation:
The yield statement pauses generator function execution and produces the value specified.

96. What is the output of the following statement? console.log(1 < 2 && 2 < 3);

a) true
b) false
c) 1
d) 3
Correct Answer: a) true
Explanation:
(1 < 2) is true, and (2 < 3) is true. Logical AND returns true.

97. What is the output of the following statement? console.log(1 < 2 && 3 < 2);

a) true
b) false
c) 1
d) 2
Correct Answer: b) false
Explanation:
(1 < 2) is true, but (3 < 2) is false. Logical AND evaluates to false.

98. What is the output of the following statement? console.log(1 > 2 || 3 < 2);

a) true
b) false
c) 1
d) 2
Correct Answer: b) false
Explanation:
Both conditions (1 > 2) and (3 < 2) evaluate to false, resolving logical OR to false.

99. What happens when executing a 'break' statement with a label pointing to a simple block statement `{ label: { break label; console.log("No"); } }`?

a) It safely jumps out of the labeled block statement.
b) SyntaxError
c) It throws a ReferenceError
d) It causes an infinite loop
Correct Answer: a) It safely jumps out of the labeled block statement.
Explanation:
Labeled break statements can be used with any block statement (not just loops), breaking out of the block early.

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

a) 1
b) 2
c) 3
d) 0
Correct Answer: a) 1
Explanation:
When i=0, count increments to 1. When i=1, break terminates loop. Count stays equal to 1.
← Previous: JavaScript Closures MCQs for Senior Developer Interviews
Next →: JavaScript DOM Manipulation MCQs
C++ Control Flow / If-Else MCQs Conditional Statements

C++ Control Flow / If-Else MCQs Conditional Statements

Control flow statements determine the execution path of a program based on runtime conditions. In C++, decision-making relies primarily on…

By MCQs Generator
JavaScript Interview Questions MCQs

JavaScript Interview Questions MCQs

JavaScript interview questions evaluate a developer’s deep understanding of core language mechanics, execution models, and advanced runtime behavior. Top tech…

By MCQs Generator
Java Advanced MCQs

Java Advanced MCQs

Advanced Java encompasses complex programming paradigms, low-level runtime mechanics, and high-performance framework design beyond standard syntax. It probes deep into…

By MCQs Generator