menu

JavaScript Conditional Statements

JavaScript Conditional Statements - Important Points


21.

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

A. x is greater than 10

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

C. x is less than or equal to 5

D. None of the above

Discuss Work Space

Answer: option c

Explanation:

As x = 5, else statement will be executed.

22. Which of the following is the correct syntax for an if statement in JavaScript?

A. if x = 10 {

B. if (x == 10)

C. if (x = 10)

D. if [x == 10]

Discuss Work Space

Answer: option b

Explanation:

The correct syntax for an if statement in JavaScript is if (condition).

23.

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

A. x is greater than y

B. y is greater than x

C. x and y are equal

D. No output

Discuss Work Space

Answer: option b

Explanation:

The condition x > y is false, so the else block is executed, which outputs "y is greater than x".

24. Which of the following is the correct syntax for a switch statement in JavaScript?

A. switch (x) {

B. switch (x == 10) {

C. switch x {

D. switch (x === "10") {

Discuss Work Space

Answer: option a

Explanation:

The correct syntax for a switch statement in JavaScript is switch (expression).

25.

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

A. x is 1

B. x is 2

C. x is not 1 or 2

D. No output

Discuss Work Space

Answer: option c

Explanation:

Since x is not 1 or 2, the default case is executed, which outputs "x is not 1 or 2".


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