JavaScript Operators

Mathematical operations

JavaScript supports all basic mathematical operations:

Build :

 
let x = 10;
let y = x + 50;

Subtraction :

 
let x = 100;
let y = x - 50;

Multiplication :

 
let x = 4;
let y = 5;
let z = x * y;

Division :

 
let x = 5;
let y = 2;
let z = x / y;
console log(z); // 2.5

Modulo division (% operator) returns the remainder of the division:

let x = 5;
let y = 2;
let z = x % y;
console log(z); // one

The result will be 1, because the largest integer less than or equal to 5 that is divisible by 2 is 4, and 5 – 4 = 1.

Exponentiation . The ** operator raises a number to a certain power:

 
number ** degree

For example:

const n = 2 ** 3;
console.log(n); // 8

const x = 3;
const y = 2;
const z = x ** y;
console.log(z); // 9

Increment :

let x = 5;
x++; // x = 6

The increment operator increments ++a variable by one. There is a prefix increment that first increments a variable by one and then returns its value. And there is a postfix increment that first returns the value of a variable and then increments it by one:

// prefix decrement
let x = 5;
let z = ++x;
console.log(x); // 6
console.log(z); // 6

// postfix decrement
let a = 5;
let b = a++;
console.log(a); // 6
console.log(b); // 5

Postfix increment is similar to the operation:

a = a + 1; // a++

A decrement decreases the value of a variable by one. There is also prefix and postfix decrement:

// prefix decrement
let x = 5;
let z = --x;
console.log(x); // 4
console.log(z); // 4

// postfix decrement
let a = 5;
let b = a--;
console.log(a); // 4
console.log(b); // 5

As is customary in mathematics, all operations are performed from left to right and differ in priority: first, increment and decrement operations, then multiplication and division, and then addition and subtraction. To change the standard flow of operations, part of the expressions can be placed in brackets:

let x = 10;
let y = 5 + (6 - 2) * --x;
console.log(y); //41

Comparison Operators

Typically, comparison operators are used to test a condition. Comparison operators compare two values ​​and return a value true or false:

  • == : The equality operator compares two values, and if they are equal, it returns true, otherwise it returns false : x == 5
  • === : The identity operator also compares two values ​​and their type, and if they are equal, returns true, otherwise returns false : x === 5
  • != : Compares two values, and if they are not equal, returns true, otherwise returns false : x != 5
  • !== : Compares two values ​​and their types, and if they are not equal, returns true, otherwise returns false : x !== 5
  • > : Compares two values, and if the first is greater than the second, then returns true, otherwise returns false : x > 5
  • < : Compares two values, and if the first is less than the second, then returns true, otherwise returns false : x < 5
  • >= : Compares two values, and if the first is greater than or equal to the second, then returns true, otherwise returns false : x >= 5
  • <= : Compares two values, and if the first is less than or equal to the second, then returns true, otherwise returns false : x <= 5

All operators are fairly simple, except perhaps for the equality operator and the identity operator. They both compare two values, but the identity operator also takes into account the type of the value. For example:

let income = 100;
let strIncome = "100";
let result = income == strIncome;
console.log(result); //true

The result variable here will be equal to true, since both income and str Income actually represent the number 100.

But the identity operator will return false in this case, since the data is of a different type:

let income = 100;
let strIncome = "100";
let result = income === strIncome;
console.log(result); // false

The inequality operators != and !== work similarly.

Boolean operations

Boolean operations are used to combine the results of two comparison operations. JavaScript has the following logical operations:

&& Returns true if both comparison operations return true, otherwise returns false:

let income = 100;
let percent = 10;
let result = income &gt; 50 &amp;&amp; percent &lt; 12;
console.log(result); //true

|| Returns true if at least one comparison operation returns true, otherwise returns false:

let income = 100;
let isDeposit = true;
let result = income &gt; 50 || isDeposit == true;
console.log(result); //true

! Returns true if the comparison operation returns false:

let income = 100;
let result1 = !(income &gt; 50);
console.log(result1); // false, because income &gt; 50 returns true

let isDeposit = false;
let result2 = !isDeposit;
console.log(result2); // true

Assignment operations =

Sets a variable to a specific value:let x = 5;

+= Addition followed by assignment of the result. For example:

let a = 23;
a += 5; // likewise a = a + 5
console.log(a); // 28

-= Subtraction followed by assignment of the result. For example:

let a = 28;
a -= 10; // likewise a = a - 10
console.log(a); // 18

*= Multiplication followed by assignment of the result:

let x = 20;
x *= 2; // likewise x = x * 2
console.log(x); // 40

**= Raising to a power and then assigning the result:

let x = 5;
x **= 2;
console.log(x); // 25

/= Division followed by assignment of the result:

 
let x = 40;
x /= 4; // likewise x = x / 4
console.log(x); // 10

%= Getting the remainder of a division and then assigning the result:

let x = 10;
x %= 3; // likewise x = x % 3
console.log(x); // 1, because 10 - 3*3 = 1

&&= a &&= b returns b if and a and b are equal true. If any of the operands is false, then returns a. Similar to doing a = a && b:

let x = true;
let y = false;
y &amp;&amp;=x;
console log(y); // false

let c = false;
let d = true;
c &amp;&amp;= d;
console log(c); // false

let a = true;
let b = true;
a &amp;&amp;=b;
console log(a); // true

lete=false;
let f = false;
e &amp;&amp;=f;
console log(e); // false

||= a ||= b is equivalent to the expression a = a || b:

let x = true;
let y = false;
y ||= x;
console log(y); // true

let a = true;
let b = true;
a ||= b;
console log(a); // true

let c = false;
let d = true;
c ||= d;
console log(c); // true

lete=false;
let f = false;
e ||= f;
console log(e); // false