# JavaScript Operators

## JavaScript Operators

We know many operators from school: addition +, multiplication *, subtraction -, and so on. In this chapter, we will talk about the aspects of operators that are not covered by school arithmetic.

## Terms: “unary”, “binary”, “operand”

There is some terminology, which you should know before going on.

• An operand means what operators are applied to. For example, in the multiplication of 4 * 2 there are two operands: the left operand is 4 and the right operand is 2.
• If an operator has a single operand it is unary. For example, theunary negation - opposites the sign of a number:
The unary operator in javascript
• If an operator has two operands it is binary. The same minus also exists in binary form:
The binary operator in javascript

There are two different operators in the examples above, which share the same symbol: the negation operator, a unary operator, and the subtraction operator.

## String concatenation, binary +

These special features of JavaScript operators are beyond school arithmetics.

Generally the plus operator + sums numbers, but if the binary + is applied to strings, merging them:

The binary operator apply to a strings in javascript
If one of the operands is a string, the other one is transformed to a string as well:
The binary operator apply to string in javascript

Both the first operand or the second can be strings. The rule is simple anyway: if one of the operands is a string, the other one is converted into a string too.

Please, note that operations run from left to right and if there are two numbers which followed by a string, they will be added before being converted to a string:

The binary plus operation in javascript

String concatenation is one of the special features of the binary plus + . Other arithmetic operators work only with numbers converting their operands to numbers (subtraction, division).

The binary operation in javascript

## Numeric conversion, unary +

The plus + operator exists in two forms: the binary form and the unary form.

The plus operator + applied to a single value and it does not do anything to numbers. But when the operand is not a number, the unary + will convert it into a number:

The unary plus operation in javascript

## Assignment =

We use the assignment operator (=) to assign a value to the JavaScript variable, it always returns a value. It is obvious for most of them like addition + or multiplication *.

The javascript assignment operator

## Remainder %

The remainder operator % does not have anything related to percents, the result of a % b is the remainder of the integer division of a by b.

The remainder % operator in javascript

## Exponentiation **

The exponentiation operator ** was recently added to the language. For a number b, the result of a ** b is a multiplied by itself b times:

The exponentiation ** operator in javascript

The operator works also for non-integer numbers:

The exponentiation ** operator in javascript

## Increment/decrement

Increasing or decreasing a number by 1 is one of the most common numerical operations. There are special operators for it:

• Increment ++ increases a variable by 1:
The increment ++ operator in javascript
• Decrement -- decreases a variable by 1:
The decrement -- operator in javascript

Increment or decrement can only be applied to variables, so If we try to use it on a value like 6++ it will give an error.

We can place the operators ++ and -- before or after a variable.

• When the operator is after the variable, it is in “postfix form” : counter++.
• When the operator is before the variable, it is in “prefix form” : ++counter.

These two statements do the same thing: increase counter by 1.

The prefix ++ increment operator

## Increment/decrement among other operators

The operators ++/-- can also be used inside expressions. Their priority is higher than most other arithmetical operations.

Let’s compare these two examples:

The prefix ++ increment operator in javascript

and

The postfix increment ++ operator in javascript

Technically they are okay, but this notation usually makes code less readable. One line does multiple things, what is not good.

During reading code, a fast “vertical” eye-scan can easily miss something like counter++ and it won’t be obvious that the variable increased. That’s why we advise a style of “one line – one action”:

The postfix increment ++ operator in javascript