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. Which of the following is a falsy value in JavaScript?

0

undefined

{}

1


3.

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

x is greater than 10

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

x is less than or equal to 5

None of the above


4. 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;


5. What is the purpose of the else statement in a conditional statement?

To provide an alternative condition to check

To define a function

To perform a specific action

To end the conditional statement


6.

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


7.

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


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

if x = 10 {

if (x == 10)

if (x = 10)

if [x == 10]


9. Which of the following is not a valid way to declare a variable in JavaScript?

var x;

let x;

const x;

int x;


10.

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