01 / 21
{}

Explaining
Code Behavior

Learn to read JavaScript line by line, trace execution step-by-step, and explain what code does using precise terminology.

JavaScript

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

Declare
Create a new variable using let, const, or var
Initialize
Assign a first value to a variable at the moment of declaration
Invoke
Call a function by writing its name followed by parentheses: fn()
Evaluate
Compute an expression down to a single value, following operator precedence

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.

1
Debugging: You cannot fix a bug if you cannot describe what the code is doing wrong.
2
Code review: Professional developers explain code to teammates every day.
3
Interviews: Technical interviews require you to walk through code out loud.

The JavaScript Execution Model

PHASE 1: CREATION Memory is allocated Variables set to undefined Functions stored entirely PHASE 2: EXECUTION Code runs line by line Values assigned to variables Expressions evaluated CALL STACK greet("Alice") Global Execution Context

The Golden Rule of Code Tracing

1
What is being stored? Is a variable declared, reassigned, or is a return value captured?
2
What operation runs? Is it arithmetic, a comparison, a function call, or a method invocation?
3
What is the resulting value? Evaluate the expression fully. State the type and value.

Expressions vs. Statements

Expression
Any code that produces a value. Can appear on the right side of =
Statement
An instruction that performs an action. It does not produce a value by itself.
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

1
Number every line. This gives you a reference to point to as you explain.
2
Create a variable table. Track each variable, its current value, and its type.
3
Walk each line. State the action in one sentence using the correct term (declare, assign, invoke, evaluate, return).
4
Track the output. Write down every console.log call's result in order.
5
State the final result. What does the program output? What value does a function return?

Example 1: Variable Assignment

JS
1let x = 5;
2let y = x * 2;
3x = x + 1;
4console.log(x, y);
1
Declare x with let and initialize it to the number 5.
2
Declare y. Evaluate x * 2: since x is 5, this produces 10. Assign 10 to y.
3
Reassign x. Evaluate x + 1: 5 + 1 produces 6. x is now 6. Note: y stays 10.
4
Invoke console.log with arguments x (6) and y (10).
Console Output
6 10

Example 2: Function Calls

JS
1function double(n) {
2  return n * 2;
3}
4let result = double(7);
5console.log(result);
1
Lines 1-3: Declare a function named double that accepts one parameter n. The function is stored in memory but not executed yet.
2
Line 4: Invoke double(7). The argument 7 is passed as n. Inside the function, evaluate 7 * 2 which produces 14. Return 14.
3
The return value 14 is assigned to result. Then console.log(result) outputs 14.
Console Output
14

Example 3: Loops and Conditionals

JS
1let total = 0;
2for (let i = 1; i <= 3; i++) {
3  if (i % 2 === 0) {
4    total += i;
5  }
6}
7console.log(total);
1
Line 1: Declare total, initialize to 0.
2
Line 2: Begin for-loop. Declare i = 1. Check condition: 1 <= 3 is true. Enter loop body.
3
Iteration 1 (i=1): Evaluate 1 % 2 === 0 which is false. Skip the if-block. Increment i to 2.
4
Iteration 2 (i=2): 2 % 2 === 0 is true. Execute total += 2: total becomes 2. Increment i to 3.
5
Iteration 3 (i=3): 3 % 2 === 0 is false. Skip. Increment i to 4. Condition 4 <= 3 is false: exit loop.
6
Line 7: Invoke console.log(total). Output: 2.
Console 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.

Line total i Output 1 0 - - 2 (init) 0 1 - 3 (i=1) 0 1 skip if 4 (i=2) 2 2 += 2 5 (i=3) 2 3 skip if 7 2 - log: 2

Scope: Where Variables Live

Global Scope let name = "Alice"; Function Scope: greet() let greeting = "Hello"; Block Scope: if { } let message = greeting + name; Can access: message, greeting, name

Return Values vs. Side Effects

Return Value
The value a function sends back to the caller via the return keyword. It can be stored in a variable.
Side Effect
Anything a function does besides returning a value: logging to console, modifying a global variable, or changing the DOM.
JS
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

✕ Vague Language
Saying "it does stuff with x" instead of "the variable x is reassigned to the value 10."
✕ Skipping Steps
Jumping to the output without tracing through each iteration of a loop or each function call.
✕ Confusing Declaration and Assignment
Saying "x is declared as 10" on a reassignment line. Declaration uses 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

What does console.log(b) output?

Question 2 of 5

Which term best describes what happens when greet("Sam") runs?

Question 3 of 5

What value does console.log(count) print?

Question 4 of 5

What does console.log(result) output, and why?

Question 5 of 5

In JavaScript, which of the following is an expression rather than a statement?