menu

JavaScript Conditional Statements


1.

What is the output of the following code?

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

x is truthy

x is falsy

0 is truthy

No output


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

var x;

let x;

const x;

int x;


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

switch (x) {

switch (x == 10) {

switch x {

switch (x === "10") {


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 purpose of the ternary operator 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


6.

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

x is greater than y

y is greater than or equal to x

undefined

Error


7.

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

10

15

20

25


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


9.

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


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