menu

JavaScript Conditional Statements

JavaScript Conditional Statements - Important Points


11.

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

var x = 10;
if (x > 5) {
x = 20;
}
console.log("Value of x is", x);

A. 10

B. 15

C. 20

D. 25

Discuss Work Space

Answer: option c

Explanation:

The condition in the if statement is true since x is greater than 5, so the code block is executed and x is assigned the value of 20.

12.

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

A. x is 10

B. x is not 10

C. undefined

D. Error

Discuss Work Space

Answer: option a

Explanation:

The condition in the if statement is true since x is equal to 10, so the code block inside the if statement is executed and "x is 10" is logged to the console.

13.

What is the output of the following code?

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

A. x is greater than 20

B. x is greater than 5 but less than or equal to 20

C. x is less than or equal to 5

D. None of the above

Discuss Work Space

Answer: option b

Explanation:

The first condition in the if statement is false since x is not greater than 20. The second condition in the else if statement is true since x is greater than 5 but less than or equal to 20, so the code block inside the else if statement is executed and "x is greater than 5 but less than or equal to 20" is logged to the console.

14.

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

A. 15

B. 20

C. 25

D. 30

Discuss Work Space

Answer: option b

Explanation:

The first condition in the if statement is false since x is not less than 5. The second condition in the else if statement is true since x is less than 15, so the code block inside the else if statement is executed and x is assigned the value of 20.

15.

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

A. x is greater than y

B. y is greater than or equal to x

C. undefined

D. Error

Discuss Work Space

Answer: option b

Explanation:

The condition in the if statement is false since x is not greater than y. The else block is executed and "y is greater than or equal to x" is logged to the console.


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now