JavaScript expression

An expression is a unit of code that can be evaluated and resolved into a value. Expressions in JS can be divided into several categories.

Arithmetic expression

In this category, take all expressions whose calculation results are numbers:

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

String expression

Expression that evaluates to a string:

'A ' + 'string'

Main expression

Under this category, variable references, literals and constants:

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

There are also some language keywords:

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 initializer expressions

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

Logical expression

Logical expressions use logical operators and are parsed as Boolean values:

a && b
a || b
!a

Left side expression

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

seeSpread operatorTutorials

Attribute access expression

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

Object creation expression

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

Function definition expression

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

Call expression

The syntax of calling a function or method

a.x(2)
window.resize()

Download mine for freeJavaScript beginner's manual


More js tutorials: