menu

JavaScript Conditional Statements


1. Which of the following is a logical operator in JavaScript?

&&

||

!

All of the above


2. What is the purpose of the switch statement in JavaScript?

To declare a new variable

To define a loop

To execute a block of code based on a condition

To assign a value to a variable


3.

What is the output of the following code?

var x = 0;
if (x) {
console.log("x is truthy");
} else {
console.log("x is falsy");
}

x is truthy

x is falsy

0 is truthy

No output


4. Which of the following is the correct syntax for a ternary operator in JavaScript?

x ? y : z;

x : y ? z;

x ? y, z;

x ? y || z;


5.

What is the output of the following code?

var x = 5;
if (x > 10) {
console.log("x is greater than 10");
} else if (x > 5) {
console.log("x is greater than 5 but less than or equal to 10");
} else {
console.log("x is less than or equal to 5");
}

x is greater than 10

x is greater than 5 but less than or equal to 10

x is less than or equal to 5

None of the above


6.

What is the output of the following code?

var x = "10";
var y = 10;
if (x == y) {
console.log("x and y are equal");
} else {
console.log("x and y are not equal");
}

x and y are equal

x and y are not equal

10 and 10 are equal

No output


7.

What is the output of the following code?

var x = 5;
var y = 10;
if (x > y) {
console.log("x is greater than y");
} else {
console.log("y is greater than or equal to x");
}

x is greater than y

y is greater than or equal to x

undefined

Error


8. Which of the following is a logical operator in JavaScript?

+

/

&&

%


9. What is the purpose of the break statement in a switch statement?

To exit the switch statement

To execute the next case

To define a new case

To execute a default case


10.

What is the value of x after the following code is executed:

var x = 10;
if (x < 5) {
x = x + 5;
} else if (x < 15) {
x = x + 10;
} else {
x = x + 15;
}
console.log("Value of x is", x);

15

20

25

30