- Define "Expression"
- Define "Evaluation"
- Expression and Evaluation with JavaScript
- Identify the Parts of an Expression
- Identify Core Operators in JavaScript
Previously, we saw a tool that allowed us to have a conversation with
JavaScript. This tool is known as a REPL, short for Read Evaluate Print
Loop. Code written in the top box will be read and evaluated. The
response, whatever is returned, will appear in the bottom box. For example,
below we have a REPL with a math equation, 10 + 10
, prewritten inside. If you
click the button with the play symbol ▶
above the equation, you should see
20
appear in the bottom box.
Note: We encourage you to experiment with this and other code examples provided in the REPL. In order to do so, you will need to click "open in repl.it" in the upper right corner of the REPL window, which will open it in a new browser tab. You will also need to create a free account. Once you've done that, you will be able to fork the REPL to your account and make changes to try things out.
Conversations, we've seen, are the things that result when two individuals — be they human or machine — communicate expressions to one another.
We've been imprecise in defining "expression" while we were getting the hang of it. Let's propose formal definitions for expression and evaluation.
An expression in a programming language is like a sentence in a spoken language.
Some sentences are simple: "He wept." Some sentences are complex: "I sing of weapons and a man, an outcast of Troy who was driven to the shores of Italy..."
Some expressions are simple: 2
. Some expressions are complex 1 + 2
. Some
expressions are really complex: 10 + (3 * ( (-1) ** 3) + 2) / 18
.
Definition: Expression: A combination of information, called data, and symbols indicating how to combine data, called operators.
Evaluation is the process of interpreting an expression, according to rules, to produce a return value.
These definitions should align with your experience of having a conversation
with REPLs so far. Think about 255 / 5
. Which parts of the expression are
data? Which parts are operators?
Pro-tip: Think it through yourself. Which is a given thing (data) and which parts tell you how to combine things (operators)? When reading technical documents you can't simply read the answers, you have to think along in order to learn. Active participation tells your brain that this stuff is important!
The data are: 255
and 5
The operator: is /
In this example, there is only one operator. It's certainly possible for
expressions to have multiple operators like 100 + 10 - 3
. In this example, the
operators are +
and -
.
Here's a table of other operators and their operations. While some of the symbols used are different from their mathematical counterparts, the operations work in the familiar way.
Operator | Operation | Note |
---|---|---|
+ |
Addition | |
- |
Subtraction | |
* |
Multiplication | We use * instead of × because it looks like x-the-letter |
/ |
Division | We use / instead of ÷ because that's not on a keyboard |
** |
Exponentiation | We use ** instead of ^ because ^ means something else in programming languages |
() |
Association | Expressions inside of () get evaluated earlier |
In the next few lessons, we're going to introduce the Essential Three Expressions:
- The constant expression
- The assignment expression (variable assignment)
- The variable lookup expression
All expressions, which are the core of every programming language, are built on these Essential Three Expressions.