Expressions

An expression is the most granular type of syntactic container. We've already been working a lot with expressions. Even expressing a literal value, like the number 1, will produce an expression:

1 // <= An expression containing the literal value 1

Using an operator also forms an expression:

'hi ' + 'there'

In fact, we can consider an operator as something that is itself applied to expressions. So the addition operator's syntax can be understood like so:

EXPRESSION + EXPRESSION

An expression can be as simple as a literal value or a variable reference, but may also be complex. The following expression encompasses a series of operations and is spread over a few lines:

(
'this is part of' +
' ' +
['a', 'very', 'long', 'expression'].join(' ')
)

Expressions are not limited to primitive types or simple literal values. Class definitions, function expressions, array literals, and object literals are all things that can appear in the context of an expression. The easy way to know whether something is an expression is the question of whether or not it can go within a group operator (that is, parentheses) without causing a SyntaxError:

(class Foo {});   // Legal Expression
(function() {}); // Legal Expression
([1, 2, 3]); // Legal Expression
({ a: 1, b: 2 }); // Legal Expression

(if (a) {}); // ! SyntaxError (Not an Expression!)
(while (x) {}); // ! SyntaxError (Not an Expression!)

The syntactic building blocks of any program involve various different layers of syntactic structures. We have individual values and references: if we zoom out a little bit, we have expressions, and if we zoom out even further we have statements, which we will now explore.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.13.164