# JavaScript language operators. Classification of operators

### Contents

Search other resources:

##### 1. The concept of operator

Operators allow you to perform various actions on data (values) and return the results of these actions. In the JavaScript language, depending on the number of operands (values), operators are divided into two groups:

• unary. These are operators that require one operand. For example, the operator – (minus), which defines a negative number: -28, -3.55;
• binary. These operators use two operands to produce a result. For example, the operators + (addition), (subtraction), * (multiply), etc.

Operands in operators can be:

• constants;
• variables;
• expressions (also contain operators);
• functions.

Operators return only one value.

Examples.

```var x = 25
var y = 37
var z

// бінарний оператор * (множення)
z = x * y

// унарний оператор - (мінус), від'ємне число
z = -37```

### ⇑

##### 2. Classification of operators

In JavaScript, operators are divided into the following groups:

• mathematical operators;
• assignment operators;
• comparison operators;
• logical operators;
• binary operators or bitwise operators;
• string operator +;
• special operators.

### ⇑

##### 3. Mathematical (arithmetic) operators

The mathematical operators are:

• – subtraction;
• * – multiplication;
• / – division;
• % – modulo division;
• ++ – increment operator;
• − decrement operator.

Example.

```// Mathematical operators
var x, y, z;
x = 2.88
y = 3
z = x + y     // z = 5.88
z = x + y + y // z = 8.88

// 2. Substraction -
z = x - y // z = -0.12
x = 5 - y // x = 2

// 3. Multiplication *
x = 8
y = 20.5
z = x * y   // z = 164

// 4. Division /
z = 10 / 5  // z = 2
z = x / 3   // z = 2.6666666666666665

// 5. Increment ++
x = 5
x++ // x = x + 1 =>   x = 6

x = 5
z = x++ // z = 5, x = 6 - postfix form

x = 5
z = ++x // x = 6, z = 6 - prefix form

// 6. Decrement --
y = 3
y-- // y = y - 1 =>   y = 2

y = 3
z = y-- // z = 2, y = 3

y = 3
z = --y // y = 3, z = 3

// 7. Divide by modulo (remainder of integer division)
x = 8
y = 3
z = x % y  // z = 2
z = 5 % 1  // z = 0
z = 3 % 8  // z = 3
z = 0 % 10 // z = 0
z = 7 % 7  // z = 0```

### ⇑

##### 4. Assignment operators

Assignment operators allow you to set values to variables, increase or decrease the values of variables by a specified value, and so on.

The list of JavaScript assignment operators includes the following:

• = – gives the variable some value;
• += – increases the value of the variable by the specified value;
• -= – decreases the value of the variable by the specified value;
• *= – multiplies the value of the variable by the specified value;
• /= – divides the value of the variable by the given value;
• %= – divides the value of the variable by the specified value and returns the remainder of the division.

Example.

```// Assignment operators
var x

// 1. Operator =
x = 120

// 2. Operator +=
x += 30 // x = x+30 =>   150

// 3. Operator -=
x -= 10 // x = x-10 =>   140

// 4. Operator *=
x = 100
x *= 2.5   // x = x*2.5 => 250

// 5. Operator /=
x = 55
x /= 3   // x = x/3 =>   18.333333333333332

// 6. Operator %=
x = 8
x %= 3   // x = x%3   => 2```

### ⇑

##### 5. Comparison operators

Comparison operators check operands for equality, inequality, and so on. The return result of a comparison operator is true or false.

JavaScript implements the following comparison operators:

• == – equality (if the operands are equal, then true is returned);
• != – not equality;
• === – strictly equal;
• !== – strictly not equal;
• > – greater than;
• >= – greater than or equal;
• < – less;
• <= – less than or equal.

Example.

```// Comparison operators
var x, y
var b

// 1. == - equal
// 1.1. Comparison of numbers
x = 3
y = 4
b = x == y // b = false

// 1.2. Comparison of strings
b = "abc" == "abc" // b = true
b = "abcd" == "abcde" // b = false

// 1.3. Comparing a number and a string
b = "5" == 5 // b = true

// 2. != - not equal
b = 6 != 6 // b = false
b = "ab" != "ba" // b = true
b = true != false // b = true
b = "2.2" != 2.2 // b = false - comparison of number and string

// 3. >, >= - greater, greater than or equal
x = 8; y = 10
b = x > y   // b = false
b = y >= x // b = true
b = y >= 10 // b = true
b = "abc" >= "abcd" // b = false

// 4. <, <= - less, less than or equal
x = 5.2; y = 3.4
b = x < y   // b = false
b = y < x   // b = true
b = y <= x  // b = true
b = x <= 5.2 // b = true
b = "aaa" <= "aaaa" // b = true

// 5. === - strictly equal
b = 5==="5" // b = false

// 6. !== - strictly not equal
b = 5!=="5" // b = true```

### ⇑

##### 6. Logical operators

Logical operators determine the truth (true) or falsity (false) of a logical expression. JavaScript has three logical operators.

1. ! – negation or logical NOT.

With logical negation, true becomes false and false becomes true.

1. && – logical AND. For two boolean type operands, using this operator produces the following result:
```false && false = false
false && true   = false
true   && false = false
true   && true = true```
1. || – logical OR. Operator || returns boolean true or false according to the following table
```false || false = false
false || true   = true
true || false = true
true || true = true```

Example.

```// Logical operators
var x, y, z

// 1. Operator ! - logical negation (NOT)
x = true
z = !x   // z = false
z = !(9<=6) // z = true

// 2. Operator && - logical AND
x = true
y = false

z = x && y // z = false
z = (5>4) && (3<=7) // z = true

// 3. Operator || - logical OR
z = true || (6>0) // z = true
z = (5<3) || (4>10) // z = false```

### ⇑

##### 7. Binary (bitwise) operators

Binary operators perform operations on the digits of numbers represented by the binary system. These operators are also called bitwise operators.

The JavaScript language contains the following binary operators:

1. ~ – binary inversion (bitwise logical negation). The values of bits 1 become 0, and, conversely, the values of bits 0 become 1.
1. & – binary (bitwise) AND. The result of processing two bits is determined by the operation of logical multiplication according to the following principle:
```0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1```
1. | – binary OR. The bits of each of the two operands interact as follows:
```0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1```
1. ^ – binary exclusive OR (XOR). The result of this operation for two given bits is formed according to the following rules
```0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 1```
1. << – shift to the left by a specified number of digits with filling of the least significant digits with zeros. By shifting to the left, you can get a number multiplied by 2n, where n is the number of digits by which the original number is shifted.
1. >> – shift to the right by the specified number of digits with filling of the most significant digits with the value of the bit of the most significant digit. The shift to the right divides the original number by 2n, where n is the number of digits by the shifted original number.
1. >>> – shift to the right without sign. This is a shift to the right by one or more bits, with the leading bits filled with zeros.

Example.

```// Binary operators
var x
var y

// 1. Operator & - logical AND
x = 10    // x = 1010
y = 7     // y = 0111
z = x & y // z = 0010   => 2

// 2. Operator | - logical OR
x = 10    // x = 1010
y = 6     // x = 0110
z = x | y // z = 1110   => 14

// 3. Operator ~ - binary inversion
x = 255   // x =    11111111
z = ~x    // z = (1)00000000 => -256

x = 1     // x =   00000001
z = ~x    // z =   11111110   => -2

// 4. Operator ^ - binary XOR
x = 12    // x = 1100
y = 6     // y = 0110
z = x ^ y // z = 1010 => 10

// 5. Operator << - shift left with zero padding
x = 3      // x = 00000011
z = x << 2 // z = 00001100   => 12

x = 5      // x = 00000101
z = x << 1 // z = 00001010   => 10

// 6. Operator >> - shift to the right with filling with the most significant bit
x = 5      // x = 00000101
z = x >> 1 // z = 00000010   => 2

x = -5     // x = 11111010 => -5
z = x >> 1 // z = 11111101   => -3

// 7. Operator >>> - right shift without sign
x = -1      // x = 111...111 - 32 bits
z = x >>> 1 // z = 011...111 => 2147483647

x = 5       // x = 000...0101 - 32 bits
z = x >>> 1```

### ⇑

##### 8. String operator +

In JavaScript, the binary operator + can be used to manipulate numbers as well as strings. The following possible situations are considered:

• if strings are placed in the left and right parts of the + operator, then the operator implements string concatenation (see the example below);
• if a string is used in one of the parts of the operator, and a number in the other, then this number is converted to a string and concatenated into a string. In other words, if at least one operand is a string, then the second operand will also be converted to a string.

Example.

```// String processing operator +
// 1. String concatenation
var s1 = "Hello"
var s2 = "world"
var s3
s3 = s1 + ", " + s2 + "!" // s3 = "Hello, world!"

// 2. String and Number Processing
s3 = "25" + 25 // s3 = "2525"
s3 = 25.8 + "25.8" // s3 = "25.825.8"```

### ⇑

##### 9. Number and string interaction operators –, *, /

Operators (minus), * (multiplication), / (division) can use numbers and strings as operands at the same time. In this case, the string value will be converted to a numeric value and the corresponding operation will be performed.

Example.

```// Operators for processing numbers and strings -, *, /
// 1. Operator - (minus)
var str = "35"
var num = 50
var res = num - str // res = 50 - 35 = 15
res = "20" - 80 // res = -60

// 2. Operator * (multiplication)
str = "200"
num = 10
res = num * str   // res = 2000
res = "5" * 23.5 // res = 117.5

// 3. Operator / (division)
res = 50 / "50" // res = 1
res = "2.5" / 10 // res = 0.25```