menu

JavaScript Conditional Statements

JavaScript Conditional Statements - Important Points


31.

What is the output of the following code?

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

A. x is truthy

B. x is falsy

C. hello is truthy

D. No output

Discuss Work Space

Answer: option a

Explanation:

The variable x has a truthy value ("hello"), so the if block is executed, which outputs "x is truthy".

32.

What is the output of the following code?

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

A. x is truthy

B. x is falsy

C. 0 is truthy

D. No output

Discuss Work Space

Answer: option b

Explanation:

The variable x has a falsy value (0), so the else block is executed, which outputs "x is falsy".

33.

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

A. x and y are equal

B. x and y are not equal

C. 10 and 10 are equal

D. No output

Discuss Work Space

Answer: option a

Explanation:

The == operator performs type coercion, so the string "10" is converted to the number 10, which is equal to the variable y. Therefore, the if block is executed, which outputs "x and y are equal".

34.

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

A. x and y are equal

B. x and y are not equal

C. 10 and 10 are equal

D. No output

Discuss Work Space

Answer: option b

Explanation:

The === operator does not perform type coercion, so the string "10" is not equal to the number 10. Therefore, the else block is executed, which outputs "x and y are not equal".

35.

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

A. x is greater than 10

B. x is less than or equal to 10

C. 5 is greater than 10

D. 5 is less than or equal to 10

Discuss Work Space

Answer: option b

Explanation:

The condition x > 10 is false, so the code block after the : symbol is executeTherefore, the variable result is assigned the value "x is less than or equal to 10", which is outputted 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