What is an Operator?
4 + 4; // 8
+ is an operator that performs addition, and
4 are operands.
Here is a list of different operators you will learn in this tutorial.
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- String Operators
Assignment operators are used to assign values to variables.
let x = 10;
= operator is used to assign a value of
10 to variable
Here’s a list of commonly used assignment operators:
|=||Assignment operator||x = y|
|+=||Addition assignment||x += y|
|-=||Subtraction Assignment||x -= y|
|*=||Multiplication Assignment||x *= y|
|/=||Division Assignment||x /= y|
|%=||Remainder Assignment||x %= y|
Note: The most commonly used assignment operator is
Arithmetic operators are used to perform arithmetic calculations. A typical arithmetic operation operates on two numbers.
let x = 30 + 50; // x = 80
Arithmetic operators perform arithmetic on numbers (literals or variables). Here’s a list of commonly used arithmetic operators:
|+||Addition||35 + 5 = 35|
|–||Subtraction||35 – 5 = 20|
|*||Multiplication||10 * 20 = 200|
|/||Division||20 / 2 = 10|
|%||Modulus||56 % 3 = 2|
|++||Increment||let a = 10; a++; Now a =11|
|– –||Decrement||let a = 10; a–; Now a = 9|
Comparison operators compare two values and return a boolean value, either true or false. You can check all types of data; comparison operators always return true or false.
const a = 4, b = 2; console.log(a > b); // true
Here, the comparison operator
> is used to compare whether
a is greater than
Here’s a list of commonly used comparison operators:
|==||Equal to||5 == 10 false|
|===||Identical (equal and of same type)||5 === 10 false|
|!=||Not equal to||5 != 10 true|
|!==||Not identical||10 !== 10 false|
|>||Greater than||10 > 5 true|
|>=||Greater than or equal||10 >= 5 true|
|<||Less than||10 < 5 false|
|<=||Less than or equal to||10 <= 5 false|
Note: When using operators, be sure that the arguments are of the same data type; numbers should be compared with numbers, strings with strings, and so on.
Logical operators, also known as Boolean operators, perform logical operations and return a boolean value, either true or false.
(4 > 2) && (10 < 15)
For this expression to be true, both conditions must be true.
- The first condition determines whether 4 is greater than 2, which is true.
- The second condition determines whether 10 is less than 15, which is also true.
- Based on these results, the whole expression is found to be true.
Here’s a list of commonly used logical operators:
|&&||Logical AND: ||x && y|
|!!||Logical OR: ||x !! y|
|!||Logical NOT: ||!x|
Note: You can check all types of data; comparison operators always return true or false.
The most useful operator for strings is concatenation, represented by the + sign.
Concatenation can be used to build strings by joining together multiple strings, or by joining strings with other types.
The above example declares and initialises two string variables, and then concatenates them.
Note: When + is used with strings, it performs concatenation. However, when + is used with numbers, it performs addition.
let x = 6 + 6; let y = "6" + 6; let z = "Hello" + 88; /* Output 12 66 Hello88 */
Note: Numbers in quotes are treated as strings. And If you add a number and a string, the result will be a string!
If you liked this article, then please share. You can also find me on Twitter for more updates.