- Identify the constant expression
- Explain how the constant expression stops evaluation
Lets repeat our definition of expression
Definition: Expression: A combination of information, called data, and symbols indicating how to combine data, called operators.
What if we were to make an expression that had no operators? What if it only
had data. For example, what if we just pass 9000
into a REPL:
Reminder: Click the "run" button to clear any old or unwanted code in your code sandbox and reset the console!
In response, we get 9000
. This type of expression is known as the constant
expression and it's very important, although very boring.
It's boring because it doesn't do anything except be itself. But it's important because it confirms that JavaScript knows when to stop applying operations. It tells JavaScript to stop, you have an answer.
Let's consider a simple arithmetic expression. Keep in mind we apply operators in "PEMDAS" order: parenthesis, exponents, multiplication, division, addition, subtraction.
We'll start with the expression:
JavaScript's mission is to find a constant piece of data or a constant
expression. Because of ()
, it goes there first. The (10 - 4)
is clearly
not a constant expression because of the -
operator's presence. JavaScript
makes a "tree" of the two sides of the operator (-
) and then looks on each
side to see whether those sides are constant expressions i.e. "plain old
data."
Since both 4
and 10
are plain old data, it can apply -
to them and produce
6
โ a constant expression.
So what JavaScript now sees looks like this:
Here again, JavaScript checks both sides of the operator *
and, because both
sides are now constant expressions, applies the *
to 3
and 6
and creates
a new constant expression, the answer (or "return value"):
Whew! Fortunately, JavaScript does all this work of building a tree of operators and returning a value very quickly!
The constant expression is always the last expression in a complex expression. It's how JavaScript knows it has data that it can work with and that no other operations need to be applied.
Another way of looking at this process might be to look at a table. We'll repeat all the same things we just showed graphically, but if a table makes more sense for you, then you'll like this one better!
This is an important strategy when learning to program: if you like thinking in code, try out the code; if you prefer diagrams, draw a diagram; if tables make more sense to you, build a table. An important part of learning to be a technologist is learning to build the tools that will help you learn.
Expression | Has Operators? | Operators | Are we done? | Next Step |
---|---|---|---|---|
3*(10-4) |
YES | * , () |
NO | Zoom in on new sub-expression in () because of PEMDAS |
(10-4) |
YES | - |
NO | Evaluate sub-expressions |
10 |
NO | NONE | YES | Zoom in on expression 10 . Constant expression! Return the value of the constant, we're done! |
4 |
NO | NONE | YES | Zoom in on expression 4 . Constant expression! Return the value of the constant, we're done! |
(10-4) |
YES | - |
NO | Replace ( 10 - 4 ) with application of - to 10 and 4 making 6 |
3*6 |
YES | * |
NO | Zoom out and replace the sub-expression with its value we just determined |
3 |
NO | NONE | YES | Zoom in on expression 3 . Constant expression! Return the value of the constant, we're done! |
6 |
NO | NONE | YES | Zoom in on expression 6 . Constant expression! Return the value of the constant, we're done! |
3*6 |
YES | * |
NO | Apply * to 3 and 6 making 18 |
18 |
NO | NONE | YES | Constant expression! Return the value of the constant, we're done! |
While the constant expression might seem dull, it lets us (and JavaScript)
know when expression evaluation is done and establishes a groundwork for all
the following expressions. The first rule of Aristotle's logic is A is A
; the
constant expression provides a similar "foundation" for programming.