menu

JavaScript Conditional Statements


1. Which of the following is a truthy value in JavaScript?

0

null

0

hello


2.

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


3. What is the purpose of the if statement in a conditional statement?

To provide an alternative condition to check

To define a function

To perform a specific action

To execute a block of code only if a certain condition is true


4.

What is the output of the following code?

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

x is equal to y

x is not equal to y

undefined

Error


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.

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. Which of the following is the correct syntax for a switch statement in JavaScript?

switch (x) {

switch (x == 10) {

switch x {

switch (x === "10") {


8.

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


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