JavaScript AcademyProfessional Engineering

Learning object 5 of 100

Mathematical expression evaluator

Type an arithmetic expression and see how JavaScript groups it, which part is evaluated first, and the value of every sub-expression. The tree is built by parsing your expression; each value is calculated by running that part of it.

Loading…

What it shows

Precedence decides the grouping (* and / before + and -), then operands are evaluated left to right. Parentheses change the grouping. The numbered circles show the order in which operations finish.

Guided experiment

  1. Compare 4 + 6 2 with (4 + 6) 2.
  2. Try 2 3 2. Exponentiation groups from the right: is it 64 or 512?
  3. Try 10 - 4 - 3. Subtraction groups from the left.

Predict

What is 80 + 90 / 2?

Related

Lessons: Lesson 3: Statements, expressions and comments, Lesson 8: Numbers and mathematical operations

Projects: Project 9: Shopping Cart Price Calculator, Project 1: Interactive Tip Calculator

Source: web/assets/js/objects/object-005.js in the companion repository.