Expression Evaluation

in JavaScript

This is an entry in Marek's JavaScript Compendium. Last updated 2020-12-23.

When the JavaScript engine is running and it encounters an expression, it evaluates the expression and produces a JavaScript value as a result.

Evaluation is an action that can happen to an expression. It takes place at a particular point in time: when the JavaScript engine reaches the expression during the execution of a program.

Expressions are always evaluated one at a time because JavaScript is a single-threaded language.

You can think of evaluation as the transformation of an expression (along with all of its inputs, at a given moment in time) into a result value.

The output of evaluation

The evaluation of an expression always produces a single result value. This means that it can never produce multiple values, and it can never not produce a value.

When the same expression is evaluated multiple times, it might produce a different value each time.

What about undefined values?

You can have an expression that produces a result which is undefined. This might sound like a case of an expression that produces no value at all, but it still produces a value.

That's because in JavaScript, undefined doesn't mean the absence of a value: it's the name of a special value.

So an expression that produces a result of undefined is a value-producing expression like any other expression, because undefined is considered a value like any other value.

What about producing multiple values?

What if an expression produces an array of values? Doesn't that contradict the claim that an expression can only produce one single value?

Yes, an expression can produce an array of values. But there's no contradiction, because the array is a single value. The other values are just the contents of that array.

Similarly, an expression can produce an object which contains multiple values inside. But the object is still considered the single value that is being produced.

Side-effects of evaluation

When an expression is evaluated, it can cause side-effects to happen.

Unlike the medical definition of a side-effect, the programming definition of a side-effect is not a bad thing. It's just an effect which isn't the main effect of the evaluation of an expression.

The main effect of evaluation is to produce a result value. So a side-effect is something that happens which is unrelated to the production of that result. For example, it can be the modification of a variable or of an object.

Inputs of the expression

Before the JavaScript machine can evaluate an expression, it typically needs to obtain some input values.

The input values of an expression come from the evaluation of its sub-expressions.

When an expression is evaluated, first its sub-expressions must be evaluated. The results of those sub-expressions then become the inputs to the main expression.

Multiple evaluations of an expression

Any expression can be evaluated any number of times (because it might be in a loop, or in a function that's called multiple times).

It's very possible that an expression will produce a different result value each time that it gets evaluated.

See Also