Dive into the intricacies of JavaScript control structures with this engaging quiz designed to test your understanding of loops, conditionals, and other fundamental programming constructs. Whether you’re a novice or a seasoned coder, these questions will help you gauge your proficiency and reinforce your knowledge. Best of luck, and may your code always run smoothly!
We recommend that you do not leave the page that you are taking this quiz in. Stay honest 🙂
JavaScript Control Structures Quiz Questions Overview
1. What does the ‘if’ statement do in JavaScript?
Executes code if a specified condition is true
Repeats a block of code a specified number of times
Defines a function
Declares a variable
2. Which of the following loops is guaranteed to execute at least once?
for loop
while loop
do…while loop
for…in loop
3. What is the purpose of the ‘break’ statement in a loop?
To pause the loop temporarily
To stop the loop and exit it
To skip the current iteration and continue with the next one
To reset the loop counter
4. Which keyword is used to skip the current iteration of a loop and continue with the next one?
break
continue
skip
return
5. How do you write an ‘else if’ statement in JavaScript?
else if (condition) { // code }
elseif (condition) { // code }
else (condition) { // code }
if else (condition) { // code }
6. What will the following code output? let x = 5; if (x > 3) { console.log(‘x is greater than 3’); } else { console.log(‘x is not greater than 3’); }
x is greater than 3
x is not greater than 3
undefined
Syntax error
7. Which loop is best suited for iterating over the properties of an object?
for loop
while loop
do…while loop
for…in loop
8. What does the ‘switch’ statement do?
Executes code based on multiple conditions
Defines a function
Declares a variable
Repeats a block of code a specified number of times
9. How do you write a ‘for’ loop that iterates from 0 to 9?
for (let i = 0; i < 10; i++) { // code }
for (let i = 0; i <= 9; i++) { // code }
for (let i = 0; i < 9; i++) { // code }
for (let i = 1; i <= 10; i++) { // code }
10. What will the following code output? let i = 0; while (i < 3) { console.log(i); i++; }
0
1
2
0
1
2
3
1
2
3
1
2
11. What is the output of the following code? for (let i = 0; i < 5; i++) { if (i === 3) { break; } console.log(i); }
0
1
2
0
1
2
3
0
1
2
3
4
0
1
2
4
12. What will the following code output? let i = 0; do { console.log(i); i++; } while (i < 3);
0
1
2
0
1
2
3
1
2
3
1
2
13. Which statement is used to handle multiple conditions in JavaScript?
if…else
switch
for
while
14. What will the following code output? let x = 10; if (x > 5) { console.log(‘x is greater than 5’); } else if (x > 8) { console.log(‘x is greater than 8’); } else { console.log(‘x is 10’); }
x is greater than 5
x is greater than 8
x is 10
Syntax error
15. How do you write a ‘while’ loop that executes as long as a variable ‘i’ is less than 10?
while (i < 10) { // code }
while i < 10 { // code }
while (i <= 10) { // code }
while (i > 10) { // code }
16. What will the following code output? for (let i = 0; i < 5; i++) { if (i === 3) { continue; } console.log(i); }
0
1
2
4
0
1
2
3
4
0
1
2
1
2
3
4
17. Which of the following correctly describes the ‘else’ statement?
Executes code if the ‘if’ condition is false
Executes code if the ‘if’ condition is true
Executes code if the ‘else if’ condition is true
Executes code if the ‘else if’ condition is false
We recommend that you do not leave the page that you are taking this quiz in. Stay honest 🙂