- JavaScript operators are symbols or keywords used to perform operations on values and variables.
- They are the building blocks of JavaScript expressions and can manipulate data in various ways.
1. Arithmetic Operator
- Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.
+= operator
The following example uses the += operator to add one to variable x:
let x = 10;
x += 1;
console.log(x); // 11
-= operator
The following example uses the -= operator to minus one from the variable x:
let x = 10;
x -= 1;
console.log(x); // 9
(*=) operator
The following example uses the *= operator to multiply 10 with the variable x:
let x = 10;
x *= 10;
console.log(x); // 100
/= operator
The following example uses the /= operator to divide x by 2 and assign the result back to x:
let x = 10;
x /= 2;
console.log(x); // 5
%= operator
The following example uses the %= operator to get the remainder of x is divided by 2 and assigns the remainder back to x:
let x = 5;
x = x % 2;
console.log(x); // 1
Chaining JavaScript assignment operator
If you want to assign a single value to multiple variables, you can chain the assignment operators. For example:
let a = 10, b = 20, c = 30;
a = b = c; // all variables are 30
In this example, JavaScript evaluates from right to left. Therefore, it does the following:
let a = 10, b = 20, c = 30;
b = c; // b is 30
a = b; // a is also 30
3. Comparison / Relational operators
Comparison operators compare two values and return a boolean (true or false). They are useful for making decisions in conditional statements.
See the following example:
console.log("10" == 10); // true
console.log("10" === 10); // false
In the first comparison, since we use the equality operator, JavaScript converts the string into a number and performs the comparison.
However, in the second comparison, we use the strict equal operator (===), JavaScript doesn’t convert the string before comparison, therefore the result is false.
SOCIAL SHARE CARD GENERATOR