Loops Flashcards
What is a loop in JavaScript?
A loop is a programming construct that repeats a block of code as long as a specified condition is true.
What keyword is used to create a ‘for’ loop in JavaScript?
The keyword used is ‘for’.
Fill in the blank: The syntax for a ‘while’ loop begins with the keyword ‘____’.
while
True or False: A ‘do…while’ loop guarantees that the code block will execute at least once.
True
What is the general syntax of a ‘for’ loop in JavaScript?
‘for (initialization; condition; increment) { // code block }’
What type of loop is best used when the number of iterations is known?
‘for’ loop
What is the difference between ‘while’ and ‘do…while’ loops?
‘while’ checks the condition before executing the block, while ‘do…while’ checks after.
Complete the following: ‘for (let i = 0; i < 5; i++) { console.log(i); }’ will log numbers ____.
0 to 4
What does the ‘break’ statement do in a loop?
It exits the loop immediately.
In a ‘for…in’ loop, what is being iterated over?
The properties of an object.
What is the purpose of the ‘continue’ statement in a loop?
It skips the current iteration and moves to the next one.
True or False: A ‘for…of’ loop can be used to iterate over arrays.
True
Fill in the blank: The ‘____’ loop can be used to iterate over iterable objects like arrays and strings.
for…of
What will be the output of the following code: ‘for (let i = 0; i < 3; i++) { console.log(i); }’?
0, 1, 2