An expression is a single unit of JavaScript code that the JavaScript engine can evaluate, and return a value.
Expressions can vary in complexity. We start from the very simple ones, called primary expressions, to more complex ones. Each different expression produces a value.
In this lesson, I introduce you to the different expression types you can find.
Primary expressions
Under this category go variable references, literals, and constants:
2
0.02
'something'
true
false
this //the current scope
undefined
i //where i is a variable or a constant
Arithmetic expressions
Under this category go all expressions that evaluate to a number:
1 / 2
i++
i -= 2
i * 2
String expressions
Expressions that evaluate to a string:
'A ' + 'string'
Array and object initializers expressions
[] //array literal
{} //object literal
[1,2,3]
{a: 1, b: 2}
{a: {b: 1}}
Logical expressions
Logical expressions make use of logical operators and resolve to a boolean value:
a && b
a || b
!a
Property access expressions
object.property //reference a property (or method) of an object
object[property]
object['property']
Object creation expressions
new object()
new a(1)
new MyRectangle('name', 2, {a: 4})
Function definition expressions
function() {}
function(a, b) { return a * b }
(a, b) => a * b
a => a * 2
() => { return 2 }
Invocation expressions
The syntax for calling a function or method
a.x(2)
window.resize()