JavaScript operators are like tools that help us perform actions on values.
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.
There are Various Types of Operators in JavaScript
1.Arithmetic Operators
Perform mathematical calculations like addition, subtraction, multiplication, etc.
| Operator | Example | Real-Life Analogy | | --- | --- | --- | |
+
(Addition) |5 + 3 = 8
| Adding money to your wallet | |-
(Subtraction) |10 - 4 = 6
| Spending money on groceries | |*
(Multiplication) |6 * 2 = 12
| Buying multiple items of the same price | |/
(Division) |10 / 2 = 5
| Splitting a bill between friends | |%
(Modulus) |10 % 3 = 1
| Finding the remaining chocolates after distributing evenly |
Example: Calculating your monthly salary
- If you earn ₹1000 per day and work for 30 days
let dailyWage = 1000;
let totalSalary = dailyWage * 30; // Multiplication
console.log(totalSalary); // Output: 30000
2. Assignment Operators
used to assign values to variables. They can also perform operations like addition or multiplication before assigning the value.
| Operator | Example | Real-Life Analogy | | --- | --- | --- | |
=
(Assign) |x = 10
| Setting initial savings | |+=
(Add and Assign) |x += 5
(x = x + 5) | Getting a salary bonus | |-=
(Subtract and Assign) |x -= 2
(x = x - 2) | Spending money on lunch |Example: Giving pocket money to your younger sibling
Before: You have ₹500 in your wallet.
After: You give ₹200 to your sibling, and now you have ₹300
let money = 500;
money -= 200; // Subtract 200 and assign back
console.log(money); // Output: 300
3. Comparison Operators
compare two values and return a boolean (true or false). They are useful for making decisions in conditional statements.
| Operator | Example | Real-Life Analogy | | --- | --- | --- | |
==
(Equal) |5 == "5"
→ true | Checking if two things look the same | |===
(Strict Equal) |5 === "5"
→ false | Checking if two things are exactly the same | |!=
(Not Equal) |10 != 5
→ true | Checking if two things are different | |>
(Greater Than) |80 > 50
→ true | Checking if you have more money than needed | |<
(Less Than) |20 < 100
→ true | Checking if you need more fuel in the car |Example: Checking if you have enough money to buy a laptop
Laptop Price: ₹50,000
Your Savings: ₹60,000
If the result is
true
, you can buy the laptop; otherwise, you need more money.let savings = 60000; let laptopPrice = 50000; console.log(savings >= laptopPrice); // Output: true
4. Logical Operators
used to perform the logical operations that determine the equality or difference between the values.
| Operator | Example | Real-Life Analogy | | --- | --- | --- | |
&&
(AND) |true && false → false
| You can go out only if it's sunny AND you’re free | |||
(OR) |returns true if at least one operand is true.
| You can go out only if it's sunny OR you can stay at home | |!
(NOT) |!true → false
| Reversing a condition (e.g., if it’s not raining, go outside) |Example: Deciding whether to go outside based on the weather
If it’s sunny AND you have free time, go for a walk
let isSunny = true; let isFree = true; console.log(isSunny && isFree); // Output: true (You can go for a walk)
If it’s raining OR too cold, stay indoors
let isRaining = false; let isCold = true; console.log(isRaining || isCold); // Output: true (Stay indoors)
5. Bitwise Operators
Perform operations on binary representations of numbers.
& performs a bitwise AND.
| performs a bitwise OR.
^ performs a bitwise XOR.
~ performs a bitwise NOT.
const res = 5 & 1; // Bitwise AND
console.log(res);
let vipPass = 1;
let entryTicket = 0;
let canEnter = vipPass | entryTicket; // 1 | 0 = 1
console.log(canEnter); // Output: 1 (Allowed)
let switchState = 1; // ON
let bulb = 0; // OFF
let newBulbState = switchState ^ bulb; // 1 ^ 0 = 1 (ON)
console.log(newBulbState); // Output: 1 (Bulb is ON)
let switchState = 1; // ON
let bulb = 0; // OFF
let newBulbState = switchState ^ bulb; // 1 ^ 0 = 1 (ON)
console.log(newBulbState); // Output: 1 (Bulb is ON)
6. Ternary Operator
shorthand for conditional statements. It takes three operands.
condition ? expression1 : expression2 evaluates expression1 if the condition is true, otherwise evaluates expression2.
const age = 18; const status = age >= 18 ? "Adult" : "Minor"; console.log(status); // adult
8. Unary Operators
operate on a single operand (e.g., increment, decrement).
| Operator | Example | Real-Life Analogy | | --- | --- | --- | |
++
(Increment) |x++
(x = x + 1) | Counting the number of books read | |--
(Decrement) |x--
(x = x - 1) | Reducing the number of candies after eating one |Example: Counting steps while walking
Before: Your step count is
10,000
.After taking one more step:
10,001
let steps = 10000; steps++; // Increase step count by 1 console.log(steps); // Output: 10001
9. Chaining Operator (?.)
operator allows safe access to deeply nested properties without throwing errors if the property doesn’t exist.
?. safely accesses a property or method.
Returns undefined if the property doesn’t exist.
const obj = { name: "Aman", address: { city: "Delhi" } };
console.log(obj.address?.city); //Delhi
console.log(obj.contact?.phone); // Undefined
10. BigInt Operators
allow calculations with numbers beyond the safe integer range.
Operations like addition, subtraction, and multiplication work with BigInt.
Use n suffix to denote BigInt literals.
const big1 = 123456789012345678901234567890n; const big2 = 987654321098765432109876543210n; console.log(big1 + big2); // 1111111110111111111011111111100n
11. Shift Operators
- used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two.
Left Shift (<<
)
Example : Doubling the Number of Seats in a Bus
If a bus has 16 seats and the owner doubles the seats, the new count is 32.
Left shift (
<<
) is like multiplying by 2.let seats = 16; let newSeats = seats << 1; // 16 * 2 = 32 console.log(newSeats); // Output: 32
Right Shift (>>
)
Example: Splitting a Pizza in Half
If you have 16 slices and divide it equally among 2 friends, each gets 8 slices.
Right shift (
>>
) is like dividing by 2let slices = 16; let perPerson = slices >> 1; // 16 / 2 = 8 console.log(perPerson); // Output: 8
Read More Articles...
I’m truly thankful for your time and effort in reading this.