Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 26
Q623 - Control Flow
Review the following code:
- for (let a = 4; a < 4; a++) {
- console.log("test");
- }
How many times will "test" be displayed in the console
as a result of its execution?
-
A
One
-
B
Four
-
C
Three
-
D
It will not be displayed at all.
Reveal correct answer
Correct answer: D
Explanation
Topics: for less than operator
postfix increment operator
Try it yourself:
- for (let a = 4; a < 4; a++) {
- console.log("test");
- }
Explanation:
The condition is never met.
In the beginning a is 4 and thereby not less than 4
and the loop never starts.
The for statement defines a code block
that is executed as long as a condition is true
https://www.w3schools.com/jsref/jsref_for.asp
The less than operator returns true if the left operand is
less than the right operand, and false otherwise.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than
The increment operator increments its operand and returns a value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment
Q623 (Please refer to this number, if you want to write me about this question.)
A. The loop will not execute at all due to the condition a < 4 not being met. Therefore, "test" will not be displayed in the console even once.
B. The loop will not execute at all due to the condition a < 4 not being met. Therefore, "test" will not be displayed in the console four times.
C. The loop will not execute at all due to the condition a < 4 not being met. Therefore, "test" will not be displayed in the console three times.
D. The condition in the for loop is set to a < 4, but the initial value of a is already 4. Therefore, the condition is not met, and the loop will not execute at all, resulting in "test" not being displayed in the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
