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:
    w3docs logo The unary operator in javascript
    let x = 2; x = -x; console.log(x); // -2, here was applied unary negation
  • If an operator has two operands it is binary. The same minus also exists in binary form:
w3docs logo The binary operator in javascript
let x = 2, y = 3; console.log(y - x); // 1, binary minus subtracts values

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:

w3docs logo The binary operator apply to a strings in javascript
let str = "my" + "string"; console.log(str); // mystring
If one of the operands is a string, the other one is transformed to a string as well:
w3docs logo The binary operator apply to string in javascript
console.log( '2' + 3 ); // "23" console.log( 3 + '2' ); // "32"

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:

w3docs logo The binary plus operation in javascript
console.log(3 + 2 + '1' ); // "51" and not "321"

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

w3docs logo The binary operation in javascript
console.log( 2 - '1' ); // 1 console.log( '6' / '2' ); // 3

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:

w3docs logo The unary plus operation in javascript
// this doesn’t effect numbers let x = 1; console.log( +x ); // 1 let y = -2; console.log( +y ); // -2 // converts not numbers console.log( +true ); // 1 console.log( +"" ); // 0

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

w3docs logo The javascript assignment operator
let x = 3 * 2 + 1; console.log( x ); // 7

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.

w3docs logo The remainder % operator in javascript
console.log( 7 % 2 ); // 1 is a remainder of 7 divided by 3 console.log( 11 % 3 ); // 2 is a remainder of 11 divided by 3 console.log( 12 % 3 ); // 0 is a remainder of 12 divided by 4

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:

w3docs logo The exponentiation ** operator in javascript
console.log( 3 ** 2 ); // 9 (3 * 3) console.log( 3 ** 3 ); // 27 (3 * 3 * 3) console.log( 3 ** 4 ); // 81 (3 * 3 * 3 * 3)

The operator works also for non-integer numbers:

w3docs logo The exponentiation ** operator in javascript
console.log( 9 ** (1/2) ); // 3 (power of 1/2 is the same as a square root) console.log( 27 ** (1/3) ); // 3 (power of 1/3 is the same as a cubic root)

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:
    w3docs logo The increment ++ operator in javascript
    let counter = 10; counter++; // it's a counter = counter + 1 // it will be shorter console.log( counter ); // 11
  • Decrement -- decreases a variable by 1:
    w3docs logo The decrement -- operator in javascript
    let counter = 10; counter--; //it's a counter = counter - 1 // it is shorter console.log( counter ); // 9

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.

w3docs logo The prefix ++ increment operator
let counter = 1; let a = ++counter; console.log(a); // 2

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:

w3docs logo The prefix ++ increment operator in javascript
let counter = 2; console.log( 2 * ++counter ); // 6

and

w3docs logo The postfix increment ++ operator in javascript
let counter = 2; console.log( 2 * counter++ ); // 4, because counter++ returns the "old" value

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

w3docs logo The postfix increment ++ operator in javascript
let counter = 1; console.log( 2 * counter );//2 counter++; console.log( counter );//2

Do you find this helpful?