Are you ready?
11 questions to solve
Instructions
- This quiz goes to full-screen once you press the Start button.
- At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
A
for
loop's header can be left all blank, like for (;;)
. True or false?A
for
loop's header is comprised of three individual sections, each of which is optional. Hence, the loop's header can be for (;;)
, which goes with choice (A). For more info, refer to JavaScript Loops — for
.What does the
continue
keyword do inside a loop?The
continue
keyword skips the current iteration and continues on with the next iteration. Hence, the correct choice is (A).The
while
loop keeps on iterating as long as the specified condition is truthy. True or false?Absolutely yes. As long as the condition of a
while
loop gets fulfilled, i.e. is truthy, it continues running. It's only when the condition fails, i.e. becomes falsey, that the loop terminates. Hence, the correct choice is (A). For more info, refer to JavaScript Loops — while
.The third part of a
for
loop's header always has to use the postfix increment (++
) operator. True or false?There is absolutely no restriction on what we use in the third part of a
for
loop's header. It is evaluated on every iteration so we can have any legal statement in there, such as i += 1
, i++
or even i = i + 10
. Hence, the given statement here is false, which goes with choice (B). For more info, refer to JavaScript Loops — for
.If programmed incorrectly, a
for
loop and a while
loop can run infinitely. True or false?By programming a loop incorrectly, we can definitely run into a loop that runs forever, until we force-terminate it by exiting the webpage running the JavaScript program containing it. Hence, the correct choice is (A).
How to get the body of a loop to be executed at least once, regardless of the loop's condition?
Regardless of the condition used, the
do...while
loop executes its body at least once. For more info, refer to JavaScript Loops — while
.How to exit out of a loop?
The
break
keyword serves to exit execution out of a loop. Refer to JavaScript Loops — for
and JavaScript Loops — while
for more info.How many times does the following loop run?
var i = 5;
for (; i < 5; i++) {
console.log(i);
}
To start with, the value of
i
is 5
, hence the condition i < 5
evaluates to false
. This means that the loop never gets executed, and this goes with choice (A). For more info, refer to JavaScript Loops — for
.Is the following code valid or not?
var i = 5;
for (i < 5; i++) {
console.log(i);
}
The code is invalid. This is because there is a missing semicolon (
;
) in the header of the for
loop. Hence, the correct choice is (B). Refer to JavaScript Loops — for
for more info.Is the following code valid or not?
var i = 5;
for (; i < 5; i++) {
console.log(i);
}
Yes, the code above is absolutely valid. The variable declaration part has been omitted, but still the code conforms to the syntax of
for
, with the added semicolon (;
), and is, likewise, valid. The correct choice is, therefore, (A). Refer to JavaScript Loops — for
for more info.What does the following code log?
for (var i = 0; i <= 3; i++) {
console.log(i);
}
The loop given above prints
i
from 0
to 3
(because 3 <= 3
also evaluates to true
). The correct choice is, therefore, (B). Refer to JavaScript Loops — for
for more info.