Operators special symbols hote hain jo values aur variables par operations perform karte hain. JavaScript mein bahut saare types ke operators hain.
+(Addition) - Jodna-(Subtraction) - Ghatana*(Multiplication) - Guna karna/(Division) - Bhag karna%(Modulus) - Remainder nikalna
Normal Division: Decimal answer deta hai
7 / 4=1.75
Floor Division: Integer answer chahiye to Math.floor() use karo
Math.floor(12 / 36)=0
Modulus (%): Remainder nikalta hai
10 % 3=1(kyunki 10 ÷ 3 = 3, bacha 1)
- Pehle value assign hoti hai, phir increment/decrement
Jaise:
x = 5,z = x--- Pehle
z = 5ho jata hai - Phir
x = 4ho jata hai
- Pehle increment/decrement, phir value assign
Example dekho:
a++→ Pehle a use hota hai, phir badhta hai++a→ Pehle a badhta hai, phir use hota hai
- Value assign karta hai
+=(Add and Assign)sm = 10; sm += 10;→ Absm = 20
-=(Subtract and Assign)*=(Multiply and Assign)/=(Divide and Assign)%=(Modulus and Assign)
Ye shortcuts hain:
sm += 10matlabsm = sm + 10
>(Greater than) - Bada hai<(Less than) - Chota hai>=(Greater than or Equal) - Bada ya barabar hai<=(Less than or Equal) - Chota ya barabar hai
Example:
chackAge >= 20→ Check karta hai age 20 ya usse zyada hai
- Sirf value check karta hai, type nahi
- Type coercion hota hai (automatic conversion)
Examples:
12 == "12"→true(type convert ho jata hai)5 == "5"→true0 == false→true1 == true→true
- Value aur Type dono check karta hai
- Type coercion NAHI hota
Examples:
12 === "12"→false(number !== string)5 === "5"→false12 === 12→true
- Loose equality ka opposite
- Strict equality ka opposite
null == undefined→true(special case!)
null === undefined→false(different types hain)
Best Practice: Hamesha === use karo confusion avoid karne ke liye!
- Strings ko alphabetically compare karta hai
"a" < "b"→true
- Uppercase letters < Lowercase letters
"Z" < "a"→true- Kyunki ASCII values mein uppercase pehle aate hain
- Strings ko jodne ke liye
"Ashish" + " " + "Choudhary"→"Ashish Choudhary"
- Dono conditions true honi chahiye
Example:
age1 >= 18 && entery→ Dono true hone par hi true
- Koi bhi ek condition true ho
- Opposite value deta hai
!true→false!false→true
Short form of if-else:
test >= 20 ? 'Adult' : 'minor'- Agar
test >= 20true hai to'Adult'warna'minor'
One liner if-else hai ye!
Simple If: Agar condition true hai to block execute hoga
If-Else: True ho to if block, false ho to else block
If-Else If-Else: Multiple conditions check kar sakte ho
Example Flow:
- Pehle strict equality check (
===) - Phir loose equality check (
==) - Phir greater than check (
>) - Else less than
Ek function jo check kare ki adult hai ya nahi
- Return type:
boolean(true/false) age >= 18→ Adult
Strong password ke liye checks:
- Empty nahi hona chahiye (
!== "") - Length kam se kam 8 characters (
length >= 8) - Dono conditions ko
&&se combine karo
Function mein return karke directly true/false mil jata hai
-
Hamesha
===use karo instead of==- Type safety milti hai
- Bugs kam hote hain
-
Ternary operator simple conditions ke liye best hai
- One liner clean code
-
Meaningful variable names use karo
chackAge→ Better:userAgechake→ Better:numberyavalue
-
Compound operators use karo shorthand ke liye
x += 5better haix = x + 5se
-
Logical operators multiple conditions combine karne ke liye
- Clean aur readable code
-
❌
==use karna (type confusion hota hai)- ✅
===use karo
- ✅
-
❌ String comparison mein case sensitivity bhulna
"Z" < "a"→true(unexpected!)
-
❌ Post vs Pre increment/decrement confuse hona
x++vs++xka difference samajho
-
❌
&&aur||ka priority bhulna- Brackets use karo clarity ke liye
-
❌ Empty string (
"") ko false samajhna- Explicitly check karo:
password !== ""
- Explicitly check karo:
| Operator Type | Symbols | Example |
| ------------------- | ------------------------ | ------------------------------- | ------ | -------- |
| Arithmetic | +, -, *, /, % | 10 + 5 |
| Increment/Decrement | ++, -- | x++, --y |
| Assignment | =, +=, -=, etc. | x += 10 |
| Comparison | >, <, >=, <= | age >= 18 |
| Equality | ==, ===, !=, !== | x === 5 |
| Logical | &&, | |, ! | a && b |
| Ternary | ? : | age >= 18 ? 'Adult' : 'Minor' |
===>==- Hamesha strict equality prefer karo- Type Coercion -
==automatically type convert karta hai - Post vs Pre - Increment/Decrement ka order important hai
- Ternary - Clean one-liner conditional ke liye
- Logical Operators - Multiple conditions combine karne ke liye powerful
- Validation Functions - Reusable code ke liye functions banao
Practice karo aur master bano! 🚀