Basic Operators

Please Refer To Common Programming Language

String Concatenation

+ operator is used to concatenate two string. If one of them is string and another one is number then the number is converted to string automatically and then the concatenation process.

let s = "my" + "string";
alert(s); // mystring

alert( '1' + 2 ); // "12"
alert( 2 + '1' ); // "21"

However, note that operations run from left to right. If there are two numbers followed by a string, the numbers will be added before being converted to a string:

alert(2 + 2 + '1' ); // "41" and not "221"

Note that, other arithmetic operators work only with numbers. They always convert their operands to numbers.

alert( 2 - '1' ); // 1
alert( '6' / '2' ); // 3

Unary Plus

The unary plus or, in other words, the plus operator + applied to a single value, doesn’t do anything with numbers, but if the operand is not a number, then it is converted into it.

// No effect on numbers
let x = 1;
alert( +x ); // 1

let y = -2;
alert( +y ); // -2

// Converts non-numbers
alert( +true ); // 1
alert( +"" );   // 0

It actually does the same as Number(...), but is shorter.

let apples = "2";
let oranges = "3";

// both values converted to numbers before the binary plus
alert( +apples + +oranges ); // 5

// the longer variant
// alert( Number(apples) + Number(oranges) ); // 5

Assignment

Let’s note that an assignment = is also an operator. It is listed in the precedence table with the very low priority of 3. That’s why when we assign a variable, like x = 2 * 2 + 1, then the calculations are done first, and afterwards the = is evaluated, storing the result in x.

let x = 2 * 2 + 1;

alert( x ); // 5

It is possible to chain assignments:

let a, b, c;

a = b = c = 2 + 2;

alert( a ); // 4
alert( b ); // 4
alert( c ); // 4

Chained assignments evaluate from right to left. First the rightmost expression 2 + 2 is evaluated then assigned to the variables on the left: c, b and a. At the end, all variables share a single value.

Assignment Operator returns a value

An operator always returns a value. That’s obvious for most of them like an addition + or a multiplication *. But the assignment operator follows that rule too.

The call x = value writes the value into x and then returns it.

Here’s the demo that uses an assignment as part of a more complex expression:

let a = 1;
let b = 2;

let c = 3 - (a = b + 1);

alert( a ); // 3
alert( c ); // 0

In the example above, the result of (a = b + 1) is the value which is assigned to a (that is 3). It is then used to subtract from 3.

Reminder %

The remainder operator % despite its look does not have a relation to percents. The result of a % b is the remainder of the integer division of a by b.

alert( 5 % 2 ); // 1 is a remainder of 5 divided by 2
alert( 8 % 3 ); // 2 is a remainder of 8 divided by 3
alert( 6 % 3 ); // 0 is a remainder of 6 divided by 3

Exponentiation **

The exponentiation operator ** is a recent addition to the language. For a natural number b, the result of a ** b is a multiplied by itself b times.

alert( 2 ** 2 ); // 4  (2 * 2)
alert( 2 ** 3 ); // 8  (2 * 2 * 2)
alert( 2 ** 4 ); // 16 (2 * 2 * 2 * 2)

The operator works for non-integer numbers of a and b as well, for instance:

alert( 4 ** (1/2) ); // 2 (power of 1/2 is the same as a square root, that's maths)
alert( 8 ** (1/3) ); // 2 (power of 1/3 is the same as a cubic root)

Increment/Decrement

Increasing or decreasing a number by one is among the most common numerical operations. So, there are special operators for that:

Increment ++ increases a variable by 1:

let counter = 2;
counter++;      // works the same as counter = counter + 1, but is shorter
alert( counter ); // 3

Decrement -- decreases a variable by 1:

let counter = 2;
counter--;      // works the same as counter = counter - 1, but is shorter
alert( counter ); // 1

Increment/decrement can be applied only to a variable. An attempt to use it on a value like 5++ will give an error.

Just like in any other language, increment and decrement operators comes with two variant. One is Postfix notation and another is Prefix notation. To know about this notation please refer to Common Programming Language.

Comma

The comma operator allows us to evaluate several expressions, dividing them with a comma ,. Each of them is evaluated, but the result of only the last one is returned.

let a = (1 + 2, 3 + 4);

alert( a ); // 7 (the result of 3 + 4)

Here, the first expression 1 + 2 is evaluated, and its result is thrown away, then 3 + 4 is evaluated and returned as the result.

Please note that the comma operator has very low precedence, lower than =, so parentheses are important in the example above. Without them: a = 1 + 2, 3 + 4 evaluates + first, summing the numbers into a = 3, 7, then the assignment operator = assigns a = 3, and then the number after the comma 7 is not processed anyhow, so it’s ignored.

Why do we need such an operator which throws away everything except the last part?

Sometimes people use it in more complex constructs to put several actions in one line.

for (a = 1, b = 3, c = a * b; a < 10; a++) {
 ...
}

Such tricks are used in many JavaScript frameworks, that’s why we mention them. But usually they don’t improve the code readability, so we should think well before writing like that.

Comparasion Operator

Please refer to Common Programming Language For Comparasion Operators.

String comparison

To see which string is greater than the other, the so-called “dictionary” or “lexicographical” order is used. In other words, strings are compared letter-by-letter.

alert( 'Z' > 'A' ); // true
alert( 'Glow' > 'Glee' ); // true
alert( 'Bee' > 'Be' ); // true