Q
What will be the output of the following JavaScript code? Code: let x = 10; while (x > 0) { console.log(x); x -= 2; }

Answer & Solution

Answer: Option A
Solution:
The code snippet uses a while loop to print even numbers from 10 to 0. It starts with x being 10 and prints the value of x to the console in each iteration, then decrements x by 2. This process continues until x becomes 0, at which point the loop stops. Therefore, the output will be: 10 8 6 4 2 0
Related Questions on Average

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? 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 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]); }

A). Prints all elements of the array numbers

B). Calculates the sum of elements in the array numbers

C). Finds the maximum element in the array numbers

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

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

Which of the following JavaScript statements is used to execute a block of code repeatedly while a condition remains true?

A). Function invocation statement

B). Conditional statement (if statement)

C). Assignment statement

D). Looping statement (while loop)

What does the following JavaScript code snippet do? Code: let x = 5; while (x > 0) { console.log(x); x -= 1; }

A). Prints even numbers from 5 to 1

B). Prints odd numbers from 5 to 1

C). Prints numbers from 5 to 0

D). Infinite loop

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 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 does the following JavaScript code snippet do? Code: let x = 10; if (x % 2 === 0) { console.log('Even'); } else { console.log('Odd'); }

A). Checks if x is even and prints 'Even'

B). Checks if x is odd and prints 'Odd'

C). Checks if x is greater than 0 and prints 'Positive'

D). Checks if x is less than 0 and prints 'Negative'

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)