/ / Expression in JavaScript: What Exactly Is an Expression?

Expression in JavaScript: What Exactly Is an Expression?

Javascript expression depicted with number thirty

An expression is any piece of code that expresses a value.

JavaScript expressions are commonly classified into two types and five categories. Let’s learn more about these classifications below.

Types of expressions in JavaScript

There are two types of JavaScript expressions: assignment and non-assignment.

What is an assignment expression?

Assignment expressions are those that assign their evaluated value to a variable (or property).

Here’s an example:

x = 3

The code in the snippet above is an assignment expression because it uses the equal (=) operator to assign its evaluated value (3) to the variable x.

What is a non-assignment expression?

Non-assignment expressions are those that do not assign their evaluated value to any variable (or property).

Here’s an example:

3 + 7

The code in the snippet above is a non-assignment expression because it doesn’t assign its evaluated value (10) to any variable (or property).

Categories of expressions in JavaScript

The five categories of JavaScript expressions are arithmetic, string, logical, primary, and left-hand side.

What is an arithmetic expression?

Arithmetic expressions are those that express a numeric value.

Here’s an example:

10

Number ten (10) in the snippet above is an arithmetic expression because it expresses a numeric value.

Consider this other example:

5 + 2

The code in the snippet above is a non-assignment arithmetic expression that expresses the numeric value of seven (7).

Here’s another example of an arithmetic expression:

z = 5 + 2

The code in the snippet above is an assignment arithmetic expression that expresses the numeric value of seven (7) and assigns it to variable z.

What is a string expression?

String expressions are those that express a string value.

Here’s an example:

"Hello"

The code in the snippet above is a string expression because it expresses a string value.

Here’s another example:

"Hello" + " " + "World"

The code in the snippet above is a string expression expressing the string value “Hello World”.

Consider this third example of a string expression:

"1234"

The code in the snippet above is a non-assignment string expression that expresses a string number value.

What is a logical expression?

Logical expressions are those that express the Boolean values, true or false.

Note

Logical expressions often involve using logical operators like &&, ||, and !.

For instance, consider this code:

100 > 7

The code in the snippet above is a logical expression because it expresses the Boolean value of true.

Consider this other example:

7 > 100

The code in the snippet above is a logical expression that expresses the Boolean value of false.

Here’s another example of a logical expression:

true

The code in the snippet above is a logical expression that expresses the Boolean value of true.

What is a primary expression?

Primary expressions are those that serve as the basis of all other expressions.

Typical types of primary expressions are variable references, literal values, and some keywords.

Here are some examples:

"Hello"          // String literal
1234             // Number literal
/(joy)/          // Regular expression literal
null             // null value
undefined        // undefined value
this             // this keyword
true             // Boolean value
x                // variable reference
function         // function keyword
class            // class keyword

Each code in the snippet above is a primary expression because they are all expressions you can use to construct other expressions.

What is a left-hand-side expression?

Left-hand-side expressions are those on the left-hand side of an assignment operator and serve as the destination of the value expressed by the right-hand expression.

Here’s an example:

x = 100 + 30

In the snippet above, x is a left-hand-side expression because it is on the left-hand of the assignment operator (=) and serves as the destination of the value expressed by 100 + 30.

Overview

This article discussed what expressions mean in JavaScript. We also looked at typical types and categories of JavaScript expressions.

Thanks for reading!

Credit

Featured Image by Johannes W

Get Notified of the Latest Publications

Similar Posts