In this series, we use JSVisualizer, a free browser-based tool, to understand classic algorithms by watching them run. Last time we looked at functions.
This time the topic is how expressions are evaluated (evaluating an expression just means working out its value), and the star of the show is the Expr view.
Where beginners get stuck
What ends up in ok here?
let a = 2;
let b = 3;
let c = 4;
let d = true;
let ok = a + b * c > 10 && d;
console.log(ok);
The answer is true. But faced with a line like line 5, it's easy to get lost:
-
Where does the calculation start?
+,*,>, and&&are all in one expression. Does it go left to right, or in some other order? -
What are the two sides of
&&? Isdpaired with10, or with all ofa + b * c > 10? -
You can't see the middle.
console.log(ok)only shows you the final result.
An expression is one line of code, but running it takes a series of small calculations. JSVisualizer's Expr view lays them out in a table, showing each part of the expression being replaced by its value, one stage at a time.
Seeing it in the Expr view
How to read it
Paste the code into JSVisualizer, press ▶ Run, and open the Expr tab. Press ▷ Human (one "human-friendly" step) four times to get through line 4. From here, switch to the finest step size, ▶ Expr. Keep pressing it until b * c has been replaced by 12 (about twelve presses):
- Each row is one stage of the expression. As you read down, parts of it are replaced by values.
- The blue row is the current stage. Pressing ▶ Expr moves you down the table. (Some presses stop on internal preparation, so the row won't always move.)
- Bold blue text is the part that will be calculated next.
- An orange background marks the part that was just replaced by a value.
-
The columns on the right show the variables in the expression. The
okcolumn only fills in on the last row.
Reading from the top
Here are just the expressions from the table:
ok = a + b * c > 10 && d
ok = 2 + b * c > 10 && d
ok = 2 + 3 * c > 10 && d
ok = 2 + 3 * 4 > 10 && d
ok = 2 + 12 > 10 && d
ok = 14 > 10 && d
ok = true && d
ok = true && true
ok = true
The variables turn into values one at a time. Then 3 * 4 becomes 12, 2 + 12 becomes 14, 14 > 10 becomes true, and finally true && true becomes true.
What the table reveals
1. Operators have precedence
In 2 + 3 * 4, the * is applied before the + to its left. The rule that decides which operator binds to which operands is called operator precedence. Here are the operators in this expression, from highest to lowest precedence:
-
*(multiply) -
+(add) -
>(compare) -
&&(and)
Since && has the lowest precedence, the whole expression means (a + b * c > 10) && d. Row 7, true && d, shows this directly: everything to the left of && has collapsed into a single value, so d is paired with the whole left side.
Note that the order in which variables are read is a separate thing from the order in which operators are applied. Look at rows 2–4: even though * has higher precedence, the variables are replaced by their values left to right, a, then b, then c. Only once the values are in place are the operators applied, * before +, following precedence.
2. You can't write a range as 0 < x < 10
When operators with the same precedence appear in a row, the rule that decides which side they group from is called associativity. < groups from the left, and that can cause a surprising bug:
let x = 15;
let inRange = 0 < x < 10;
console.log(inRange);
x is 15, so "greater than 0 and less than 10" should be false. But this prints true. Press ▷ Human once, then keep pressing ▶ Expr until you reach the row true < 10 (about eight presses) to see why:
0 < 15 becomes true first, which leaves the comparison true < 10. JavaScript treats true as 1 when comparing, so that's true too. Math notation doesn't carry over as is, and the Expr view lets you catch the odd step in the middle.
The correct version is 0 < x && x < 10.
3. The right side of && may never run
In this next program, null is a special value meaning "no value":
let user = null;
if (user !== null && user.age >= 20) {
console.log("yes");
} else {
console.log("no");
}
Press ▷ Human once, then keep pressing ▶ Expr until the whole condition becomes false (about nine presses):
Look at row 3, false && user.age >= 20. The right side of && is still in its original form. On the next row, the whole thing jumps straight to false.
With boolean values like these, && gives true only when both sides are true, so if the left side is false, the right side can't change the answer, and JavaScript doesn't evaluate it. This is called short-circuit evaluation. || (or) works the same way: if the left side is true, the right side is skipped.
(Strictly speaking, && and || work with non-boolean values too: they check whether the left side counts as true or false and return the value of one of the operands. For example, 0 && 123 is 0, and "hello" || 123 is "hello".)
Swapping the order makes the difference obvious. Change the condition to user.age >= 20 && user !== null and run it again, and the program stops with an error:
null isn't an object, so you can't access properties on it. That's why the error happens as soon as the program tries to evaluate null.age. Checking user !== null && ... first is a way of saying only calculate the right side when it's safe.
Try it yourself
Open the link below and press ▶ Run; the Expr view opens. Use ▷ Human (the h key) to reach the line you want, then step through it with ▶ Expr (the → key) and back with ◀ Expr (←).
▶ Open in JSVisualizer (order of evaluation, Expr view)
To change the code, press ✏ Edit. Before each run, predict the rows in the middle of the table:
- Change
a + b * cto(a + b) * c. Which part is calculated first now? - Change
&& dto|| d. Doesdget evaluated?
Two more programs to play with:
-
▶ Range check (0 < x < 10): what happens with
x = -5? Is there anyxthat makes0 < x < 10false? -
▶ Short-circuit evaluation: change
let user = null;tolet user = { age: 25 };. How does the table change?
Wrapping up
- The Expr view shows parts of an expression being replaced by values, stage by stage. Bold blue is what's next; orange is what just changed.
- Operator precedence decides which operator is applied first, but variables are still read left to right.
- Comparison operators like
<group from the left (associativity). Thetrue < 10in the middle explains why0 < x < 10misbehaves. -
&&and||skip the right side when the left side already decides the result. The order of your conditions matters.
Next up: nested loops. We'll run a multiplication table and a prime check in the Heatmap view and see, in color, how many times each line ran.




Top comments (0)