JavaScript Quiz: Loops and Functions

Test your knowledge of JavaScript arrays, loops, and functions with this interactive quiz.

0/10 answered
Arrays

1 What will be the output of the following code?

let sum = 0;
for (let i = 1; i <= 5; i++) {
    sum += i;
}
console.log(sum);
        
Loops

2 What is the output of the following while loop?

let i = 0;
while (i < 3) {
    console.log(i);
    i++;
}
        
Loops

3 What will the output of the following code be?

let i = 0;
for (i = 0; i < 3; i++) {
    console.log(i);
    while (i == 1) {
        console.log("Inside while loop");
        break;
    }
}
        
Methods

4 What is the output of the following code?

let str = "apple,banana,cherry";
let arr = str.split(",");
console.log(arr);
        
Methods

5 Which of the following will join the array ["apple", "banana", "cherry"] into a string "apple-banana-cherry"?

Arrays

6 What will be the result of the following code?

let arr = [1, 2, 3, 4, 5];
arr[3] = 10;
console.log(arr);
        
Functions

7 Which of the following correctly defines a function named "greet" that logs "Hello, World!"?

Functions

8 Which of the following is the correct way to call a function named "initialize()" when the page loads?

Loops

9 The following code produces an infinite loop. What is wrong with it?

let i = 0;
for (i = 0; i < 10;) {
    console.log(i);
}
        
Methods

10 The following code doesn't produce the correct result. What is wrong?

let text = "apple-banana-cherry";
let result = text.split("-");
console.log(result);
        
0

Answer Key

Back to Learning Resources