Expressions are essential units of code in JavaScript that can be evaluated and resolve to a specific value. In JavaScript, expressions can be categorized into several types, each serving a different purpose. Understanding these categories is crucial for writing effective and efficient JavaScript code. Let’s explore the different types of expressions in JavaScript.

Arithmetic Expressions

Arithmetic expressions in JavaScript involve mathematical operations and evaluate to a number. Examples of arithmetic expressions include:

1 / 2
i++
i -= 2
i * 2

String Expressions

String expressions in JavaScript evaluate to a string. They are often used to manipulate and concatenate strings. A common example of a string expression is:

'A ' + 'string'

Primary Expressions

Primary expressions in JavaScript include variables, literals, constants, and some language keywords. They represent the most basic building blocks of code. Examples of primary expressions include:

2
0.02
'something'
true
false
this // the current object
undefined
i // where i is a variable or a constant

Primary expressions also include certain language keywords and constructs, such as:

function
class
function* // the generator function
yield // the generator pauser/resumer
yield* // delegate to another generator or iterator
async function* // async function expression
await // async function pause/resume/wait for completion
/pattern/i // regex
() // grouping

Array and Object Initializers Expressions

Array and object initializer expressions in JavaScript are used to create arrays and objects. Examples of array and object initializer expressions include:

[] // array literal
{} // object literal
[1, 2, 3]
{a: 1, b: 2}
{a: {b: 1}}

Logical Expressions

Logical expressions in JavaScript make use of logical operators and resolve to a boolean value (either true or false). Common logical expressions include:

a && b
a || b
!a

Left-hand-side Expressions

Left-hand-side expressions in JavaScript involve the left side of an assignment. They are used to create new instances, call methods, or reference properties. Examples of left-hand-side expressions include:

new // create an instance of a constructor
super // calls the parent constructor
...obj // expression using the spread operator

Note: To learn more about the spread operator, check out our spread operator tutorial.

Property Access Expressions

Property access expressions in JavaScript are used to reference properties (or methods) of an object. They can be accessed using dot notation or square brackets. Examples of property access expressions include:

object.property // reference a property (or method) of an object
object[property]
object['property']

Object Creation Expressions

Object creation expressions in JavaScript involve creating new instances of objects. Examples of object creation expressions include:

new object()
new a(1)
new MyRectangle('name', 2, {a: 4})

Function Definition Expressions

Function definition expressions in JavaScript define functions. They can be written using various syntaxes. Examples of function definition expressions include:

function() {}
function(a, b) { return a * b }
(a, b) => a * b
a => a * 2
() => { return 2 }

Invocation Expressions

Invocation expressions in JavaScript are used to call a function or method. They specify the arguments to be passed to the function. Examples of invocation expressions include:

a.x(2)
window.resize()

Understanding JavaScript expressions and their categories is crucial for writing concise and effective code. By utilizing these expressions appropriately, you can harness the power of JavaScript and build robust applications.