menu

JavaScript Conditional Statements


1. What is the syntax of the if statement in JavaScript?

if (condition) {code block}

if {code block} (condition)

(condition) if {code block}

{code block} if (condition)


2.

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 x");
}

x is greater than y

y is greater than x

x and y are equal

No output


3.

What is the output of the following code?

var x = 5;
switch (x) {
case 1:
console.log("x is 1");
break;
case 2:
console.log("x is 2");
break;
default:
console.log("x is not 1 or 2");
break;
}

x is 1

x is 2

x is not 1 or 2

No output


4.

What is the output of the following code?

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

x is less than 5

x is less than 10

x is greater than or equal to 10

No output


5.

What is the output of the following code?

var x = 10;
if (x === 10) {
console.log("x is 10");
} else {
console.log("x is not 10");
}

x is 10

x is not 10

undefined

Error


6. Which of the following is a comparison operator in JavaScript?

!

||

==

*


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

condition ? true : false;

true : false ? condition;

true ? condition : false;

condition : true ? false;


8.

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


9.

What is the output of the following code snippet?

var x = 5;
var result = x > 10 ? "x is greater than 10" : "x is less than or equal to 10";
console.log(result);

x is greater than 10

x is less than or equal to 10

5 is greater than 10

5 is less than or equal to 10


10.

What is a conditional statement in JavaScript?

A statement that is always executed

A statement that is executed only if a certain condition is met

A statement that is executed multiple times

A statement that defines a function