Operatorlar
JavaScript'da operatorlar qanday ishlashini o'rganing: equality, arithmetic, assignment, comparison, logical operators va nullish coalescing.
8-dars
🧠 Operatorlar
1️⃣ Equality Operators — == vs ===
== (Loose equality)
👉 Type coercion qiladi (tipni o'zgartiradi)
5 == "5" // true
false == 0 // true
null == undefined // true
JS ichida yashirin konvertatsiya bo'ladi.
Qoidasi:
Agar turlar har xil bo'lsa → JS ularni bir xil tipga o'tkazadi.
=== (Strict equality)
👉 Type coercion YO'Q
5 === "5" // false
false === 0 // false
Qoidasi:
Ham qiymat, ham type bir xil bo'lishi kerak.
⚠️ Eng muhim edge case:
NaN === NaN // false
Nega?
Chunki NaN hech qachon hech narsaga teng emas, o'ziga ham.
To'g'ri tekshirish:
Number.isNaN(NaN) // true
🧮 2️⃣ Arithmetic Operators
| Operator | Nima qiladi |
|---|---|
+ |
qo'shish |
- |
ayirish |
* |
ko'paytirish |
/ |
bo'lish |
% |
qoldiq |
** |
daraja |
+ operatorining xavfli joyi
"5" + 2 // "52"
"5" - 2 // 3
Nega?
+string ko'rsa → concatenation qiladi- Boshqa operatorlar → numberga aylantiradi
🔄 3️⃣ Assignment Operators
=
+=
-=
*=
/=
%=
**=
Misol:
let x = 5
x += 3 // x = x + 3
📈 4️⃣ Increment / Decrement
++x // pre
x++ // post
Farqi:
let x = 5
console.log(x++) // 5
console.log(x) // 6
let y = 5
console.log(++y) // 6
👉 Post oldingi qiymatni qaytaradi
👉 Pre yangi qiymatni qaytaradi
🔍 5️⃣ Comparison Operators
>
<
>=
<=
Lekin bu yerda ham coercion bor:
"5" > 3 // true
String numberga o'tadi.
🧩 6️⃣ Logical Operators
&&
||
!
Lekin JS'da ular faqat boolean qaytarmaydi!
&&
👉 Birinchi falsy qiymatni qaytaradi
true && "Hello" // "Hello"
false && "Hello" // false
||
👉 Birinchi truthy qiymatni qaytaradi
null || "Hi" // "Hi"
Falsy qiymatlar:
false
0
-0
0n
""
null
undefined
NaN
❓ 7️⃣ Nullish Coalescing ??
|| bilan aralashmasin!
0 || 10 // 10
0 ?? 10 // 0
?? faqat:
nullundefined
bo'lsa ishlaydi
❓ 8️⃣ Optional Chaining ?.
user?.profile?.name
Agar biror joy null bo'lsa → error bermaydi.
🧠 9️⃣ typeof Operator
typeof 5 // "number"
typeof "hi" // "string"
typeof null // "object" ⚠️ bug
typeof null === "object" — bu JS tarixiy xato.
📊 Xulosa
| Operator | Tavsif |
|---|---|
| == | Loose equality — type coercion qiladi |
| === | Strict equality — type va qiymatni tekshiradi |
| + | String bo'lsa concat, aks holda qo'shadi |
| ++x / x++ | Pre-increment vs Post-increment |
| && | Birinchi falsy qiymatni qaytaradi |
| || | Birinchi truthy qiymatni qaytaradi |
| ?? | Faqat null/undefined uchun ishlaydi |
| ?. | Optional chaining — error bermaydi |
| typeof | Tipni aniqlaydi (null bug'i bor) |
💡 Amaliy maslahatlar
===ishlating — aniq taqqoslash uchun??vs\|\|—??faqat null/undefined uchun,\|\|barcha falsy uchun- Optional chaining — nested object'lar bilan ishlashda xavfsiz
- typeof null — bu bug,
nulltekshirish uchun=== nullishlating - NaN tekshirish —
Number.isNaN()ishlating,=== NaNishlamaydi
🔍 Qo'shimcha misollar
// Equality
5 == "5" // true (coercion)
5 === "5" // false (strict)
NaN === NaN // false
Number.isNaN(NaN) // true
// Arithmetic
"5" + 2 // "52" (concatenation)
"5" - 2 // 3 (number conversion)
10 % 3 // 1 (remainder)
2 ** 3 // 8 (exponentiation)
// Assignment
let x = 5
x += 3 // x = 8
x *= 2 // x = 16
// Increment/Decrement
let a = 5
console.log(a++) // 5 (returns old value)
console.log(a) // 6
let b = 5
console.log(++b) // 6 (returns new value)
// Comparison
"5" > 3 // true (coercion)
"10" < "2" // true (string comparison)
// Logical
true && "Hello" // "Hello"
false && "Hello" // false
null || "Hi" // "Hi"
0 || 10 // 10
0 ?? 10 // 0
// Nullish Coalescing
null ?? "default" // "default"
undefined ?? "default" // "default"
0 ?? "default" // 0
"" ?? "default" // ""
// Optional Chaining
const user = { profile: { name: "John" } }
user?.profile?.name // "John"
user?.profile?.age // undefined (no error)
user?.address?.city // undefined (no error)
// typeof
typeof 5 // "number"
typeof "hi" // "string"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" ⚠️ bug
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"
📝 Xulosa
==type coercion qiladi,===qilmaydi+operatori string bo'lsa concatenation qiladi++xpre-increment,x++post-increment&&va||boolean qaytarmaydi, qiymat qaytaradi??faqat null/undefined uchun ishlaydi?.optional chaining — error bermayditypeof null === "object"— JS tarixiy bug'iNaN === NaNfalse,Number.isNaN()ishlating