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:
  • If an operator has two operands it is binary. The same minus also exists in binary form:

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:

If one of the operands is a string, the other one is transformed to a string as well:

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:

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).

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:

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 *.

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.

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 operator works also for non-integer numbers:

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:
  • Decrement — decreases a variable by 1:

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.

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:

and

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”:


Leave a Reply

Your email address will not be published. Required fields are marked *