Operator Overview
Operators are used for JavaScript’s arithmetic expressions, comparison expressions, logical expressions, assignment expressions, and more. Table 4-2 summarizes the operators and serves as a convenient reference.
Note that most operators are represented by punctuation
characters such as + and =. Some, however, are represented by
keywords such as delete and
instanceof. Keyword operators are
regular operators, just like those expressed with punctuation; they
simply have a less succinct syntax.
Table 4-2 is organized by operator precedence. The operators listed first have higher precedence than those listed last. Operators separated by a horizontal line have different precedence levels. The column labeled A gives the operator associativity, which can be L (left-to-right) or R (right-to-left), and the column N specifies the number of operands. The column labeled Types lists the expected types of the operands and (after the → symbol) the result type for the operator. The subsections that follow the table explain the concepts of precedence, associativity, and operand type. The operators themselves are individually documented following that discussion.
Table 4-2. JavaScript operators
| Operator | Operation | A | N | Types |
|---|---|---|---|---|
++ | Pre- or post-increment | R | 1 | lval→num |
-- | Pre- or post-decrement | R | 1 | lval→num |
- | Negate number | R | 1 | num→num |
+ | Convert to number | R | 1 | num→num |
~ | Invert bits | R | 1 | int→int |
! | Invert boolean value | R | 1 | bool→bool |
delete | Remove a property | R | 1 | lval→bool |
typeof | Determine type of operand | R | 1 | any→str |
void | Return undefined value ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access