LEARNING JAVASCRIPT - Trang 105

in a value is an expression, it makes sense that variables, constants, and literals are all

expressions.

Operators

You can think of operators as the “verb” to an expression’s “noun.” That is, an expres‐

sion is a thing that results in a value; an operator is something you do to produce a

value. The outcome in both cases is a value. We’ll start our discussion of operators

with arithmetic operators: however you may feel about math, most people have some

experience with arithmetic operators, so they are an intuitive place to start.

Operators take one or more operands to produce a result. For

example, in the expression 1 + 2, 1 and 2 are the operands and + is

the operator. While operand is technically correct, you often see

operands called arguments.

Arithmetic Operators

JavaScript’s arithmetic operators are listed in

Table 5-1

.

Table 5-1. Arithmetic operators

Operator Description

Example

+

Addition (also string
concatenation)

3 + 2 // 5

-

Subtraction

3 - 2 // 1

/

Division

3/2 // 1.5

*

Multiplication

3*2 // 6

%

Remainder

3%2 // 1

-

Unary negation

-x // negative x; if x is 5, -x will be -5

+

Unary plus

+x // if x is not a number, this will attempt conversion

++

Pre-increment

++x // increments x by one, and evaluates to the new
value

++

Post-increment

x++ // increments x by one, and evaluates to value of x
before the increment

Operators | 81