0
0
Javascriptprogramming~5 mins

If–else statement in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if–else statement in JavaScript?
An if–else statement lets the program choose between two paths: one if a condition is true, and another if it is false.
Click to reveal answer
beginner
Write the basic syntax of an if–else statement in JavaScript.
if (condition) {<br> // code runs if condition is true<br>} else {<br> // code runs if condition is false<br>}
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
The code inside the if block is skipped, and the program continues after the if statement without running any alternative code.
Click to reveal answer
intermediate
Can you use multiple else if blocks in JavaScript? What is their purpose?
Yes, multiple else if blocks let you check several conditions in order, running the code for the first true condition.
Click to reveal answer
beginner
Explain with an example how if–else helps in decision making in code.
Example:<br>if (temperature > 30) {<br> console.log('It is hot');<br>} else {<br> console.log('It is not hot');<br>}<br>This helps the program decide what message to show based on temperature.
Click to reveal answer
What will the following code print?<br>if (5 > 10) { console.log('Yes'); } else { console.log('No'); }
AYes
BNothing
CNo
DError
Which keyword is used to check an additional condition after an if statement?
Aelse if
Belse
Celseif
Delif
What happens if the condition in an if statement is true?
ANothing happens
BThe else block runs
CThe program stops
DThe code inside the if block runs
Can an if statement exist without an else block?
ANo, else is required
BYes, else is optional
COnly if the condition is true
DOnly if the condition is false
What is the output of this code?<br>if (false) { console.log('A'); } else if (true) { console.log('B'); } else { console.log('C'); }
AB
BNo output
CC
DA
Describe how an if–else statement controls the flow of a program.
Think about how the program chooses what code to run based on a question.
You got /4 concepts.
    Write a simple if–else statement in JavaScript that checks if a number is positive or not.
    Use console.log to show messages for both cases.
    You got /4 concepts.