Explaining
Code Behavior
Learn to read JavaScript line by line, trace execution step-by-step, and explain what code does using precise terminology.
What Does "Explaining Code" Mean?
Explaining code behavior means you can take a block of JavaScript, read it line by line, and describe exactly what happens at each step: which values are stored, which operations run, and what the final output will be.
Key Vocabulary You Need
let, const, or varfn()Why This Skill Matters
Being able to trace and explain code is the foundation of debugging, collaboration, and writing better programs. Without it, you are guessing.
The JavaScript Execution Model
The Golden Rule of Code Tracing
Expressions vs. Statements
=| Expression | Statement | |
|---|---|---|
| Example | 3 + 4 |
if (x > 0) { } |
| Returns a value? | Yes | No |
| Can assign? | let y = 3 + 4 |
Cannot |
How to Trace Code Step-by-Step
console.log call's result in order.Example 1: Variable Assignment
1let x = 5; 2let y = x * 2; 3x = x + 1; 4console.log(x, y);
x with let and initialize it to the number 5.y. Evaluate x * 2: since x is 5, this produces 10. Assign 10 to y.x. Evaluate x + 1: 5 + 1 produces 6. x is now 6. Note: y stays 10.console.log with arguments x (6) and y (10).Example 2: Function Calls
1function double(n) { 2 return n * 2; 3} 4let result = double(7); 5console.log(result);
double that accepts one parameter n. The function is stored in memory but not executed yet.double(7). The argument 7 is passed as n. Inside the function, evaluate 7 * 2 which produces 14. Return 14.14 is assigned to result. Then console.log(result) outputs 14.Example 3: Loops and Conditionals
1let total = 0; 2for (let i = 1; i <= 3; i++) { 3 if (i % 2 === 0) { 4 total += i; 5 } 6} 7console.log(total);
total, initialize to 0.i = 1. Check condition: 1 <= 3 is true. Enter loop body.1 % 2 === 0 which is false. Skip the if-block. Increment i to 2.2 % 2 === 0 is true. Execute total += 2: total becomes 2. Increment i to 3.3 % 2 === 0 is false. Skip. Increment i to 4. Condition 4 <= 3 is false: exit loop.console.log(total). Output: 2.Tracing with a Variable Table
A variable table is the most powerful tracing tool. Create columns for each variable and update them as each line executes.
Scope: Where Variables Live
Return Values vs. Side Effects
return keyword. It can be stored in a variable.1function add(a, b) { 2 console.log("Adding!"); // side effect 3 return a + b; // return value 4} 5let sum = add(3, 4);
Common Mistakes When Explaining Code
let/const.Terminology Quick Reference
| Term | Meaning | Example |
|---|---|---|
| Declare | Create a new variable | let x |
| Initialize | First value at declaration | let x = 5 |
| Assign / Reassign | Set a new value | x = 10 |
| Evaluate | Compute to a single value | 3 + 4 → 7 |
| Invoke / Call | Execute a function | greet() |
| Return | Send value back to caller | return 7 |
Question 1 of 5
let a = 3; let b = a + 2; a = 10; console.log(b);
What does console.log(b) output?
b was initialized, a was 3, so b = 3 + 2 = 5. Reassigning a later does not change b. Variables store values, not references to other variables.Question 2 of 5
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Sam"));
Which term best describes what happens when greet("Sam") runs?
greet("Sam"), we invoke (call) the function, passing "Sam" as the argument. The function was already declared on lines 1-3. Inside, it evaluates the concatenation and returns the result.Question 3 of 5
let count = 0;
for (let i = 0; i < 4; i++) {
count += i;
}
console.log(count);
What value does console.log(count) print?
Question 4 of 5
function mystery(x) {
if (x > 10) {
return "big";
}
return "small";
}
let result = mystery(7);
console.log(result);
What does console.log(result) output, and why?
x = 7. The condition 7 > 10 evaluates to false, so the if-block is skipped. Execution continues to return "small". That return value is assigned to result.Question 5 of 5
In JavaScript, which of the following is an expression rather than a statement?
3 + 4 * 2 is an expression because it evaluates to a single value (11). The others are statements: if and for are control-flow statements, and let y = 10 is a declaration statement.