JavaScript Operators: The Basics You Need to Know
Operators in JavaScript

Introduction
When you begin learning JavaScript, one of the most important concepts you encounter is operators. Operators are fundamental because they allow your program to perform actions such as calculations, comparisons, and logical decisions.
Every real-world applicationβwhether it is an e-commerce website, authentication system, or dashboard relies heavily on operators.
In this blog, we will move step by step:
From basic understanding
To real-world usage
To practical coding patterns
What Are Operators?
Operators are symbols used to perform operations on values (operands).
let result = 10 + 5;
In this example:
10and5are operands+is the operatorThe result is
15
Operators tell JavaScript what action needs to be performed.
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder) |
Basic Example
let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.33
console.log(a % b); // 1
Real-World Example: Shopping Cart Calculation
In an e-commerce application, arithmetic operators are used to calculate total price:
let price = 499;
let quantity = 2;
let discount = 100;
let total = (price * quantity) - discount;
console.log("Total price:", total);
This is exactly how most online platforms calculate totals before checkout.
Modulus Operator in Real Use
console.log(10 % 2); // 0
console.log(7 % 2); // 1
Use cases:
Checking even or odd numbers
Alternating UI elements (for example, table rows)
Pagination logic
2. Comparison Operators
Comparison operators compare two values and return a boolean (true or false).
| Operator | Meaning |
|---|---|
| == | Equal (loose comparison) |
| === | Strict equal |
| != | Not equal |
| > | Greater than |
| < | Less than |
Basic Example
console.log(5 > 3); // true
console.log(2 < 1); // false
Critical Concept: == vs ===
console.log(5 == "5"); // true
console.log(5 === "5"); // false
==performs type conversion===checks both value and type
Real-World Problem
let input = "0";
if (input == 0) {
console.log("Accepted");
}
This may lead to unexpected behavior because "0" is converted to 0.
Best Practice
Always prefer strict comparison:
if (input === 0) {
// safer comparison
}
Real-World Example: Authentication
let enteredPassword = "1234";
let actualPassword = 1234;
if (enteredPassword === actualPassword) {
console.log("Login successful");
} else {
console.log("Invalid credentials");
}
This prevents logical and security issues.
3. Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Meaning | ||
|---|---|---|---|
| && | AND | ||
| OR | |||
| ! | NOT |
AND Operator (&&)
All conditions must be true.
let age = 20;
let hasID = true;
if (age > 18 && hasID) {
console.log("Access granted");
}
Real-world usage:
Verification systems
Entry conditions
Form validation
OR Operator (||)
At least one condition must be true.
let isAdmin = false;
let isEditor = true;
if (isAdmin || isEditor) {
console.log("Access granted");
}
Real-world usage:
Role-based permissions
Feature access control
NOT Operator (!)
Reverses a boolean value.
let isLoggedIn = false;
if (!isLoggedIn) {
console.log("Please log in");
}
Truth Tables
AND (&&)
| A | B | Result |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
OR (||)
| A | B | Result |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
4. Assignment Operators
Assignment operators are used to assign and update values.
| Operator | Meaning |
|---|---|
| = | Assign value |
| += | Add and assign |
| -= | Subtract and assign |
Example
let balance = 1000;
balance += 500; // deposit
balance -= 200; // withdrawal
console.log(balance); // 1300
Real-World Use Case
These operators are commonly used in:
Banking systems
Counters (likes, views)
Inventory updates
Mini Project: Combining All Operators
Letβs simulate a small real-world scenario.
Problem
Calculate total price
Apply discount if user is eligible
Display final result
Solution
let price = 200;
let quantity = 3;
let age = 22;
let isMember = true;
// Step 1: Calculate total
let total = price * quantity;
// Step 2: Apply discount
if (isMember && age > 18) {
total -= 100;
}
// Step 3: Output result
console.log("Final amount:", total);
Common Mistakes
1. Using == instead of ===
// Incorrect
if (value == "10")
// Correct
if (value === "10")
2. Unexpected Type Conversion
console.log("5" + 2); // "52"
JavaScript converts numbers to strings in this case.
3. Misunderstanding Logical Conditions
if (true && false) // always false
Understanding truth tables is important.
Operator Categories Summary
| Category | Purpose |
|---|---|
| Arithmetic | Perform calculations |
| Comparison | Compare values |
| Logical | Combine conditions |
| Assignment | Update values |
Conclusion
Operators are the foundation of JavaScript logic. They allow you to:
Perform calculations
Make decisions
Control application flow
Without operators, writing meaningful programs would not be possible.
I write articles on blog.prakashtsx.com and also post development-related content on:



