0
0
Javascriptprogramming~5 mins

If statement in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if statement in JavaScript?
An if statement lets the program decide to run some code only if a condition is true. It helps the program make choices.
Click to reveal answer
beginner
Write the basic syntax of an if statement in JavaScript.
The basic syntax is:<br>
if (condition) {<br>  // code to run if condition is true<br>}
Click to reveal answer
beginner
What happens if the condition in an if statement is false?
If the condition is false, the code inside the if block does not run. The program skips it and continues.
Click to reveal answer
beginner
How do you add an alternative action if the if condition is false?
You use an else block after the if block. It runs when the if condition is false.<br>
if (condition) {<br>  // runs if true<br>} else {<br>  // runs if false<br>}
Click to reveal answer
intermediate
Can you check multiple conditions with if statements? How?
Yes, by using else if blocks between if and else. It checks conditions one by one until one is true.<br>
if (cond1) {<br>  // code<br>} else if (cond2) {<br>  // code<br>} else {<br>  // code<br>}
Click to reveal answer
What does this code do?<br>
if (5 > 3) { console.log('Yes'); }
APrints 'Yes' because 5 is greater than 3
BPrints nothing because 5 is not greater than 3
CGives an error
DPrints 'No'
Which keyword runs code when the if condition is false?
Aelse
Belseif
Cthen
Dwhen
What will this code print?<br>
if (false) { console.log('A'); } else { console.log('B'); }
AA
BNothing
CB
DError
How do you check more than two conditions in an if statement?
AUse multiple <code>else</code> blocks
BUse multiple <code>else if</code> blocks
CUse <code>switch</code> only
DUse multiple <code>if</code> without else
What is the output of:<br>
if (10 === '10') { console.log('Match'); } else { console.log('No match'); }
AMatch
BUndefined
CError
DNo match
Explain how an if statement works in JavaScript and give a simple example.
Think about how you decide to do something only if a condition is true.
You got /4 concepts.
    Describe how to use else if and else to handle multiple choices in JavaScript.
    Imagine choosing between many options, checking one by one.
    You got /4 concepts.