
In JavaScript, operators are symbols or keywords used to perform operations on values and variables.
Think of operators as the tools of logic.
Variables hold data, but operators make the data move, compare, decide, and calculate.
Example:
let a = 10;
let b = 5;
console.log(a + b);
Output:
15
Here:
+is an operatoraandbare operands
Types of Operators
Today we focus deeply on:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
Like the حساب (hisab/calculation) of mathematics.
A. Addition +
Adds numbers.
let a = 10;
let b = 5;
console.log(a + b);
Output:
15
String Concatenation
+ also joins strings.
let firstName = "Anas";
let lastName = "Khan";
console.log(firstName + " " + lastName);
Output:
Anas Khan
B. Subtraction -
let a = 20;
let b = 7;
console.log(a - b);
Output:
13
C. Multiplication *
let price = 50;
let quantity = 4;
console.log(price * quantity);
Output:
200
D. Division /
let total = 100;
let students = 5;
console.log(total / students);
Output:
20
E. Modulus %
Returns the remainder after division.
console.log(10 % 3);
Output:
1
Logic
10 / 3 = 3 remainder 1
So:
10 % 3 => 1
Real-Life Use
Checking even or odd numbers:
let num = 8;
console.log(num % 2);
Output:
0
If remainder is 0, number is even.
F. Exponentiation **
Power operator.
console.log(2 ** 3);
Output:
8
Logic:
2 × 2 × 2 = 8
G. Increment ++
Increases value by 1.
let x = 5;
x++;
console.log(x);
Output:
6
Pre Increment
let x = 5;
console.log(++x);
Output:
6
First increases, then prints.
Post Increment
let x = 5;
console.log(x++);
console.log(x);
Output:
5
6
First prints, then increases.
H. Decrement --
Decreases value by 1.
let x = 10;
x--;
console.log(x);
Output:
9
2. Assignment Operators
Assignment operators assign values to variables.
A. Simple Assignment =
let age = 22;
Means:
age gets value 22
B. Add and Assign +=
let marks = 50;
marks += 10;
console.log(marks);
Output:
60
Logic:
marks = marks + 10
C. Subtract and Assign -=
let money = 100;
money -= 30;
console.log(money);
Output:
70
D. Multiply and Assign *=
let x = 5;
x *= 4;
console.log(x);
Output:
20
Logic:
x = x * 4
E. Divide and Assign /=
let x = 100;
x /= 5;
console.log(x);
Output:
20
F. Modulus and Assign %=
let x = 10;
x %= 3;
console.log(x);
Output:
1
3. Comparison Operators
Comparison operators compare two values.
They always return:
true
or
false
A. Equal To ==
Checks value only.
console.log(5 == "5");
Output:
true
Why?
Because == converts datatype automatically.
B. Strict Equal ===
Checks value AND datatype.
console.log(5 === "5");
Output:
false
Because:
5→ number"5"→ string
Datatypes differ.
Important Industry Rule
Always prefer:
===
instead of:
==
because strict comparison avoids hidden bugs.
Professional developers strongly prefer strict equality.
C. Not Equal !=
console.log(10 != 5);
Output:
true
D. Strict Not Equal !==
console.log(10 !== "10");
Output:
true
Because datatype differs.
E. Greater Than >
console.log(20 > 10);
Output:
true
F. Less Than <
console.log(5 < 2);
Output:
false
G. Greater Than or Equal >=
console.log(10 >= 10);
Output:
true
H. Less Than or Equal <=
console.log(7 <= 5);
Output:
false
4. Logical Operators
Logical operators combine conditions.
These operators build the decision-making power of programming.
A. Logical AND &&
Returns true only if BOTH conditions are true.
Truth Table
| Condition 1 | Condition 2 | Result |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
Example
let age = 20;
let hasID = true;
console.log(age >= 18 && hasID);
Output:
true
Because BOTH are true.
Real-Life Logic
Entry allowed only if:
- Age ≥ 18 AND
- Has ID card
B. Logical OR ||
Returns true if ANY one condition is true.
Truth Table
| Condition 1 | Condition 2 | Result |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Example
let isAdmin = false;
let isEditor = true;
console.log(isAdmin || isEditor);
Output:
true
Because one condition is true.
C. Logical NOT !
Reverses the boolean value.
Example
let isLoggedIn = true;
console.log(!isLoggedIn);
Output:
false
Combining Comparison + Logical Operators
This is where real programming begins.
Example
let age = 25;
let salary = 50000;
console.log(age > 18 && salary > 30000);
Output:
true
Operator Precedence
JavaScript follows mathematical priority rules.
Example
console.log(10 + 5 * 2);
Output:
20
Why?
Because multiplication happens first:
5 * 2 = 10
10 + 10 = 20
Using Brackets
console.log((10 + 5) * 2);
Output:
30
Short-Circuit Logic
Very important concept.
AND &&
Stops when first false condition appears.
console.log(false && true);
Output:
false
Second condition not checked.
OR ||
Stops when first true condition appears.
console.log(true || false);
Output:
true
Practical Real-World Example
Login System
let username = "anas";
let password = "12345";
console.log(username === "anas" && password === "12345");
Output:
true
Mini Project Example
Even or Odd Checker
let number = 7;
if (number % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
Output:
Odd
Summary Table
| Category | Operators |
|---|---|
| Arithmetic | + - * / % ** ++ -- |
| Assignment | = += -= *= /= %= |
| Comparison | == === != !== > < >= <= |
| Logical | `&& |
Golden Rule for JavaScript Developers
Always remember:
- Use
===instead of== - Use logical operators carefully
- Understand precedence
- Practice with real conditions
- Operators are the backbone of decision-making
A programmer who understands operators deeply writes cleaner logic, fewer bugs, and stronger applications.
Like grammar gives structure to language, operators give structure to programming thought.