Expressions and operators

Objectives

Expressions

Expressions in JavaScript are any code that resolves to a value. There are two types of expressions in JavaScript. Expressions may simply evaluate to a value, such as 8, "Hello", and 7 + 2. There are also assignment expressions that set a variable to a value and which evaluate to that value. Examples of the second type of expression are num = 7 + 2 and greeting = "Hello".

Operators

Operators are used to form expressions that include arithmetic, comparison, and many other functions. Operators in Java can be divided into three types and seven categories. Operators also have a precedence, which means that some operations will take precedence over other operations. For example, multiplication takes precedence over addition and subtraction. You can use parentheses to change the order of evaluation. Mozilla has a good reference for operator precedence.

Operator types:

Operator categories:

Operator precedence table

Precedence  Operators
1. []
2()
3++ --
4! ~ (unary) + (unary) - typeof void delete
5* / %
6+ -
7<< >> >>>
8< <= > >= in instanceof
9== != === !==
10&
11^
12|
13&&
14||
15?:
16yield
17= += -= *= /= %= <<= >>= >>>= &= ^= |=
18,

Useful resources for expressions and operators

Mozilla has a good article on expressions and operators. Wikibooks has a good article on operators