Bird
Raised Fist0
Javascriptprogramming~5 mins

If statement in Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 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:
if (condition) {
// code to run if condition is true
}
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.
if (condition) {
// runs if true
} else {
// runs if false
}
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.
if (cond1) {
// code
} else if (cond2) {
// code
} else {
// code
}
Click to reveal answer
What does this code do?
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?
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:
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.

      Practice

      (1/5)
      1. What does an if statement do in JavaScript?
      easy
      A. Declares a variable
      B. Repeats code multiple times
      C. Defines a new function
      D. Runs code only if a condition is true

      Solution

      1. Step 1: Understand the purpose of an if statement

        An if statement checks a condition and runs code only when that condition is true.
      2. Step 2: Compare options with the definition

        Only Runs code only if a condition is true matches this behavior exactly.
      3. Final Answer:

        Runs code only if a condition is true -> Option D
      4. Quick Check:

        If statement = runs code if true [OK]
      Hint: If it runs code only when true, it's an if statement [OK]
      Common Mistakes:
      • Confusing if with loops
      • Thinking if declares variables
      • Mixing if with function definitions
      2. Which of the following is the correct syntax for an if statement in JavaScript?
      easy
      A. if condition { console.log('Yes'); }
      B. if (condition) console.log('Yes');
      C. if condition: console.log('Yes');
      D. if (condition) then { console.log('Yes'); }

      Solution

      1. Step 1: Recall JavaScript if syntax

        The condition must be inside parentheses, and the code block can be inside curly braces or a single statement without braces.
      2. Step 2: Check each option

        if (condition) console.log('Yes'); correctly uses parentheses around the condition and a statement without braces.
      3. Final Answer:

        if (condition) console.log('Yes'); -> Option B
      4. Quick Check:

        Parentheses around condition = correct syntax [OK]
      Hint: Condition must be in parentheses for if [OK]
      Common Mistakes:
      • Omitting parentheses around condition
      • Using colons instead of braces
      • Adding 'then' keyword (not in JS)
      3. What will be the output of this code?
      let x = 10;
      if (x > 5) {
        console.log('Big');
      } else {
        console.log('Small');
      }
      medium
      A. Small
      B. 10
      C. Big
      D. No output

      Solution

      1. Step 1: Evaluate the condition x > 5

        Since x is 10, which is greater than 5, the condition is true.
      2. Step 2: Determine which block runs

        The if block runs and prints 'Big'. The else block is skipped.
      3. Final Answer:

        Big -> Option C
      4. Quick Check:

        10 > 5 is true, so output is 'Big' [OK]
      Hint: Check if condition is true to pick output [OK]
      Common Mistakes:
      • Choosing else output when condition is true
      • Confusing variable value with output
      • Ignoring else block
      4. Find the error in this code:
      if x == 5 {
        console.log('Equal');
      }
      medium
      A. Missing parentheses around condition
      B. Using double equals instead of triple equals
      C. Missing semicolon after console.log
      D. Curly braces should be parentheses

      Solution

      1. Step 1: Check if statement syntax

        In JavaScript, the condition must be inside parentheses after if.
      2. Step 2: Identify the error

        The code misses parentheses around x == 5, causing a syntax error.
      3. Final Answer:

        Missing parentheses around condition -> Option A
      4. Quick Check:

        Condition needs parentheses in if [OK]
      Hint: Always put condition inside parentheses in if [OK]
      Common Mistakes:
      • Omitting parentheses around condition
      • Confusing == and === (both valid but different)
      • Thinking semicolon is mandatory after console.log
      5. You want to print 'Adult' if age is 18 or more, otherwise 'Minor'. Which code correctly uses an if statement?
      hard
      A. if (age >= 18) { console.log('Adult'); } else { console.log('Minor'); }
      B. if age >= 18 then { console.log('Adult'); } else { console.log('Minor'); }
      C. if (age > 18) { console.log('Adult'); } else { console.log('Minor'); }
      D. if (age = 18) { console.log('Adult'); } else { console.log('Minor'); }

      Solution

      1. Step 1: Check condition for age 18 or more

        The condition should be age >= 18 to include 18 and above.
      2. Step 2: Verify syntax and logic

        if (age >= 18) { console.log('Adult'); } else { console.log('Minor'); } uses correct syntax and condition. if age >= 18 then { console.log('Adult'); } else { console.log('Minor'); } uses invalid 'then'. if (age > 18) { console.log('Adult'); } else { console.log('Minor'); } excludes age 18. if (age = 18) { console.log('Adult'); } else { console.log('Minor'); } uses assignment (=) instead of comparison (== or >=).
      3. Final Answer:

        if (age >= 18) { console.log('Adult'); } else { console.log('Minor'); } -> Option A
      4. Quick Check:

        Use >= and correct syntax for if-else [OK]
      Hint: Use >= for 18 or more, no 'then' keyword [OK]
      Common Mistakes:
      • Using assignment (=) instead of comparison
      • Using 'then' keyword (not in JS)
      • Using > instead of >= to include 18
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard