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

Answer & Solution

Answer: Option A
Solution:
The statement uses nested ternary conditional operators (? :) to assign different values to result based on the value of x. If x is greater than 0, it assigns 'Positive' to result. If x is less than 0, it assigns 'Negative' to result. Otherwise, it assigns 'Zero' to result. This allows for conditional assignment of values based on multiple conditions in a single statement.
Related Questions on Average

Which of the following JavaScript statements is used to execute a block of code repeatedly until a specified condition becomes false?

A). Function declaration statement

B). Conditional statement (if statement)

C). Assignment statement

D). Looping statement (do-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? 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? let result = x > 0 ? 'Positive' : 'Non-positive';

A). Assigns the value 'Positive' to result if x is greater than 0, otherwise assigns 'Non-positive'

B). Checks if x is greater than 0

C). Prints 'Positive' if x is greater than 0, otherwise prints 'Non-positive'

D). None of the above

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 i = 1; do { console.log(i); i++; } while (i < 3);

A). 1 2 3

B). 1 2 3 4

C). 2 3 4

D). Infinite loop

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 = 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'

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 i = 3; do { console.log(i); i--; } while (i > 0);

A). 3 2 1

B). 2 1 0

C). 3 2 1 0

D). 1 2 3