Q
What does the following JavaScript code snippet do? Code: let numbers = [1, 2, 3, 4, 5]; for (let i = 0; i < numbers.length; i++) { console.log(numbers[i]); }

Answer & Solution

Answer: Option A
Solution:
The code snippet uses a for loop to iterate through all elements of the array numbers and prints each element to the console. It starts with i being 0 and iterates until i is less than the length of the array (numbers.length). In each iteration, it accesses the element at index i using numbers[i] and logs it to the console. Therefore, the output will be: 1 2 3 4 5
Related Questions on Average

What does the following JavaScript code snippet do? Code: let result = x > 0 ? 'Positive' : (x < 0 ? 'Negative' : 'Zero');

A). Assigns 'Positive' to result if x is greater than 0, 'Negative' if x is less than 0, otherwise 'Zero'

B). Checks if x is greater than 0 and assigns 'Positive', otherwise checks if x is less than 0 and assigns 'Negative', otherwise assigns 'Zero'

C). Assigns 'Positive' to result if x is less than 0, 'Negative' if x is greater than 0, otherwise 'Zero'

D). None of the above

What does the following JavaScript code snippet do? Code: let numbers = [1, 2, 3, 4, 5]; let sum = 0; for (let num of numbers) { sum += num; } console.log(sum);

A). Finds the maximum number in the array numbers

B). Calculates the average of numbers in the array numbers

C). Sums all numbers in the array numbers

D). Checks if all numbers in the array numbers are even

What is the purpose of the following JavaScript statement? continue;

A). Exits the current loop or switch statement

B). Skips the current iteration in a loop

C). Assigns a value to a variable

D). Continues to the next iteration in a loop

What will be the output of the following JavaScript code? Code: let count = 0; do { console.log(count); count++; } while (count < 3);

A). 0 1 2 3

B). 0 1 2

C). 1 2 3

D). Infinite loop

What will be the output of the following JavaScript code? Code: let i = 0; for (; i < 3; i++) { console.log(i); }

A). 0 1 2 3

B). 0 1 2

C). 1 2 3

D). 1 2 3 4

What will be the output of the following JavaScript code? Code: for (let i = 0; i <= 3; i++) { console.log(i); }

A). 0 1 2 3

B). 0 1 2

C). 1 2 3

D). 1 2 3 4

Which of the following JavaScript statements is used to execute different code based on a specified condition?

A). Function declaration statement

B). Assignment statement

C). Conditional statement (if statement)

D). Looping statement (for loop)

What is the purpose of the following JavaScript statement? return result;

A). Exits the current loop or switch statement

B). Skips the current iteration in a loop

C). Exits the current function and returns a value

D). Continues to the next iteration in a loop

What is the purpose of the following JavaScript statement? break;

A). Assigns a value to a variable

B). Exits the current loop or switch statement

C). Skips the current iteration in a loop

D). Continues to the next iteration in a loop

What is the purpose of the following JavaScript statement? let name = 'John';

A). Declare a function

B). Declare a variable

C). Print a message to the console

D). Loop through an array