What are Operators?
Operators are special symbols or keywords used to perform operations on values and variables in JavaScript.
For example:
let a = 10;
let b = 5;
let result = a + b;
Here, + is an operator. It is used to add the values of a and b.
JavaScript provides different types of operators for performing calculations, assigning values, comparing values, checking conditions, and more.
Types of Operators in JavaScript
The commonly used operators in JavaScript are:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Increment and Decrement Operators
- Ternary Operator
- Bitwise Operators
- Nullish Coalescing Operator
- Optional Chaining Operator
- Type Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
console.log(2 ** 3); // 8
Common Arithmetic Operators
-
+→ Addition -
-→ Subtraction -
*→ Multiplication -
/→ Division -
%→ Remainder -
**→ Exponentiation
How it works
Arithmetic operators take numerical values and perform mathematical operations on them.
For example:
let price = 500;
let quantity = 3;
let total = price * quantity;
Here, * multiplies the price by the quantity:
500 × 3 = 1500
2. Assignment Operators
Assignment operators are used to assign a value to a variable or update its existing value.
let balance = 1000;
Here, = assigns 1000 to balance.
Common Assignment Operators
-
=→ Assign -
+=→ Add and assign -
-=→ Subtract and assign -
*=→ Multiply and assign -
/=→ Divide and assign -
%=→ Remainder and assign
For example:
let balance = 1000;
balance += 500;
This is the same as:
balance = balance + 500;
The new balance becomes 1500.
3. Comparison Operators
Comparison operators are used to compare two values. They return either true or false.
let age = 20;
console.log(age > 18); // true
console.log(age < 18); // false
console.log(age === 20); // true
Common Comparison Operators
-
==→ Equal -
===→ Strictly equal -
!=→ Not equal -
!==→ Strictly not equal -
>→ Greater than -
<→ Less than -
>=→ Greater than or equal -
<=→ Less than or equal
Important Difference
5 == "5"; // true
5 === "5"; // false
== allows type conversion, while === checks both the value and the data type.
4. Logical Operators
Logical operators are used to combine or reverse conditions.
There are three main logical operators:
-
&&→ AND -
||→ OR -
!→ NOT
AND &&
Both conditions must be true.
let age = 25;
let hasTicket = true;
console.log(age >= 18 && hasTicket);
Both conditions are true, so the result is true.
OR ||
At least one condition must be true.
let hasEmail = true;
let hasPhone = false;
console.log(hasEmail || hasPhone);
The result is true because hasEmail is true.
NOT !
The ! operator reverses a Boolean value.
let isLoggedIn = true;
console.log(!isLoggedIn);
Output:
false
Real-world usage
Logical operators are commonly used for:
- Login systems
- Permission checking
- Form validation
- Access control
- Multiple conditions
5. Increment and Decrement Operators
The ++ operator increases a value by 1, while the -- operator decreases a value by 1.
let count = 5;
count++;
console.log(count); // 6
count--;
console.log(count); // 5
How it works
count++;
is similar to:
count = count + 1;
And:
count--;
is similar to:
count = count - 1;
6. Ternary Operator
The ternary operator is a short way of writing a simple if...else condition.
Its syntax is:
condition ? valueIfTrue : valueIfFalse;
Example:
let age = 20;
let result = age >= 18 ? "Adult" : "Minor";
console.log(result);
Output:
Adult
How it works
First, JavaScript checks:
age >= 18
If it is true, it returns "Adult".
If it is false, it returns "Minor".
\
7. Bitwise Operators
Bitwise operators work with the binary representation of numbers.
Common bitwise operators include:
-
&→ Bitwise AND -
|→ Bitwise OR -
^→ Bitwise XOR -
~→ Bitwise NOT -
<<→ Left shift -
>>→ Right shift -
>>>→ Unsigned right shift
For example:
let a = 5;
let b = 3;
console.log(a & b);
JavaScript converts the numbers into binary and performs the operation on their bits.
Bitwise operators are mainly used in specific situations such as low-level programming, binary operations, flags, and certain performance-related tasks.
Beginners usually do not need to use these operators frequently in everyday JavaScript development.
8. Nullish Coalescing Operator
The ?? operator is used to provide a default value when the left side is null or undefined.
let username = null;
let name = username ?? "Guest";
console.log(name);
Output:
Guest
Here, username is null, so JavaScript uses "Guest".
If the value exists:
let username = "Abishek";
let name = username ?? "Guest";
console.log(name);
Output:
Abishek
9. Optional Chaining Operator
The ?. operator is used to safely access properties or methods when a value might be null or undefined.
Consider:
let user = {};
If we try:
console.log(user.address.city);
JavaScript can produce an error because address does not exist.
Instead, we can use:
console.log(user.address?.city);
The result will be:
undefined
How it works
The ?. operator checks whether the value before it exists.
If it exists, JavaScript continues accessing the property.
If it is null or undefined, JavaScript stops and returns undefined instead of throwing an error.
10. Type Operators
JavaScript provides operators that help us check or work with types and objects.
typeof
typeof is used to find the data type of a value.
let age = 25;
console.log(typeof age);
Output:
number
Another example:
let name = "Abishek";
console.log(typeof name);
Output:
string
Real-world usage
typeof is useful when we need to check the type of a value.
Conclusion
Operators are an important part of JavaScript because they allow us to perform calculations, assign and update values, compare values, combine conditions, make decisions, work with objects, and handle optional data.
The main operator types are:
- Arithmetic → Perform calculations
- Assignment → Assign or update values
- Comparison → Compare values
- Logical → Combine conditions
- Increment/Decrement → Increase or decrease values
- Ternary → Write simple conditions
- Bitwise → Work with binary values
-
Nullish Coalescing → Provide defaults for
nullorundefined - Optional Chaining → Safely access nested values
- Type Operators → Check types and object relationships
Top comments (0)