What is the correct syntax for an `if` statement in JavaScript?
A
if x > 5 then {} B
if (x > 5) {} C
if x > 5 {} D
if x > 5:
Analysis & Theory
Correct syntax uses parentheses: `if (condition) { ... }`.
What keyword follows an `if` block to run code when the condition is false?
A
elseif B
otherwise C
else D
fallback
Analysis & Theory
`else` is used to run code when the `if` condition is false.
Which of the following executes when `x = 5`?
```javascript
if (x > 5) {
console.log("Greater");
} else {
console.log("Not Greater");
}```
A
"Greater" B
"Not Greater" C
Nothing D
Error
Analysis & Theory
`x = 5` is not greater than 5, so the `else` block runs.
What is the purpose of `else if`?
A
To handle multiple conditions B
To end a loop C
To check for equality D
To assign a value
Analysis & Theory
`else if` allows checking another condition if the first `if` is false.
How many `else if` blocks can be used after an `if`?
A
Only one B
Up to 3 C
Any number D
None
Analysis & Theory
You can use any number of `else if` blocks after an `if`.
What will this print?
```javascript
if (0) {
console.log("Yes");
} else {
console.log("No");
}```
A
"Yes" B
"No" C
Nothing D
Error
Analysis & Theory
`0` is falsy, so the `else` block runs.
What value is considered truthy?
A
null B
0 C
"hello" D
undefined
Analysis & Theory
Non-empty strings like `'hello'` are truthy in JavaScript.
Can `if` blocks be nested?
A
No B
Yes, but only once C
Yes, any number of times D
Only with switch statements
Analysis & Theory
`if` blocks can be nested inside each other any number of times.
What happens if no condition in an `if` or `else if` block is true and there's no `else`?
A
Last block runs B
JavaScript throws an error C
Nothing runs D
It logs 'undefined'
Analysis & Theory
If no condition is met and there's no `else`, nothing executes.
What does this code output?
```javascript
let x = 10;
if (x < 5) {
console.log("Small");
} else if (x < 15) {
console.log("Medium");
} else {
console.log("Large");
}```
A
"Small" B
"Medium" C
"Large" D
undefined
Analysis & Theory
`x = 10` is less than 15, so 'Medium' is printed.