0
0
Javascriptprogramming~10 mins

If–else statement in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If–else statement
Start
Evaluate condition
Execute 'if' block
End
Execute 'else' block
End
The program checks a condition. If true, it runs the 'if' block; otherwise, it runs the 'else' block.
Execution Sample
Javascript
let age = 18;
if (age >= 18) {
  console.log('Adult');
} else {
  console.log('Minor');
}
This code checks if age is 18 or more and prints 'Adult' if true, otherwise 'Minor'.
Execution Table
StepCondition (age >= 18)ResultBranch TakenOutput
118 >= 18trueif blockAdult
2N/AN/AEndProgram ends
💡 Condition is true at step 1, so 'if' block runs and program ends after.
Variable Tracker
VariableStartAfter Step 1Final
age181818
Key Moments - 2 Insights
Why does the program print 'Adult' and not 'Minor'?
Because at step 1 in the execution table, the condition 'age >= 18' is true, so the 'if' block runs and the 'else' block is skipped.
What happens if the condition is false?
If the condition was false, the program would run the 'else' block instead, as shown by the branch taken in the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 1?
AError
BAdult
CMinor
DNo output
💡 Hint
Check the 'Output' column at step 1 in the execution table.
At which step does the program decide which block to run?
AStep 2
BBefore Step 1
CStep 1
DAfter Step 2
💡 Hint
Look at the 'Condition' and 'Branch Taken' columns in the execution table.
If age was 16, what would the output be according to the tables?
AMinor
BAdult
CNo output
DError
💡 Hint
Think about the condition 'age >= 18' and which branch runs when false.
Concept Snapshot
if (condition) {
  // code runs if condition true
} else {
  // code runs if condition false
}
Checks condition once, runs one block only.
Full Transcript
This visual execution shows how an if-else statement works in JavaScript. The program starts by checking the condition 'age >= 18'. If true, it runs the code inside the 'if' block and prints 'Adult'. If false, it runs the 'else' block and prints 'Minor'. The execution table traces each step, showing the condition result, which branch runs, and the output. The variable tracker shows the value of 'age' stays the same. Key moments clarify why the program chooses one block over the other. The quiz tests understanding by asking about outputs and decision steps.