1. What is the basic syntax of an if statement in JavaScript?
Answer:
Explanation:
The basic syntax of an if statement in JavaScript includes the if keyword, followed by a condition in parentheses, and then a block of statements in curly braces.
2. How does JavaScript evaluate the condition in an if statement?
Answer:
Explanation:
The condition in an if statement is evaluated as a boolean, meaning it executes if the condition is true and doesn't execute if the condition is false.
3. What happens if the condition in an if statement is false?
Answer:
Explanation:
If the condition in an if statement evaluates to false, the statements within its block are not executed and the program continues with the next block of code.
4. What is the purpose of the else block in an if-else statement?
Answer:
Explanation:
The else block is used to execute statements when the if condition evaluates to false.
5. Can an else statement be used without an if statement?
Answer:
Explanation:
An else statement must be used in conjunction with an if statement. It executes a block of code when the if statement's condition evaluates to false.
6. What is the purpose of the else if statement in JavaScript?
Answer:
Explanation:
The else if statement is used to specify a new condition to test, if the first condition is false.
7. How many else if blocks can be used with an if statement?
Answer:
Explanation:
You can use as many else if blocks as needed after an if statement to check multiple conditions in sequence.
8. What is the correct syntax for an if-else if-else statement?
Answer:
Explanation:
The correct syntax includes the if keyword, followed by a condition, then the else if keyword with another condition, and finally an else block.
9. What is a common use case for nested if statements?
Answer:
Explanation:
Nested if statements are used when you need to check a series of conditions where each subsequent condition depends on the previous one being true.
10. What type of value does the condition in an if statement expect?
Answer:
Explanation:
The condition in an if statement expects a boolean value. Non-boolean values provided as conditions are converted to boolean values (truthy or falsy).
11. What does the '===' operator check in an if condition?
Answer:
Explanation:
The '===' operator checks for strict equality, meaning both the value and the type must be the same.
12. Can you use logical operators in an if condition?
Answer:
Explanation:
Logical operators like AND (&&), OR (||), and NOT (!) can be used within if conditions to combine or invert conditions.
13. How is the if-else statement different from the ternary operator?
Answer:
Explanation:
The ternary operator is a concise way to perform assignments based on a condition, whereas the if-else statement is used for executing different blocks of code based on a condition.
14. What happens if the condition in an if-else statement is neither true nor false?
Answer:
Explanation:
If the condition in an if-else statement is not true (or truthy), the else block is executed.
15. Can you omit curly braces in a single-line if statement?
Answer:
Explanation:
In JavaScript, if there is only one statement to be executed in an if or else block, you can omit the curly braces. However, using braces is recommended for better readability.