Lesson 7 / الدرس 7
True, false, and comparing things / صح وخطأ ومقارنة الأشياء
Every decision a program makes comes down to one value: true or false. Producing that value is what comparison operators do — and one of them in JavaScript should simply never be used.
كل قرار يتخذه برنامج يعود إلى قيمة واحدة: صح أو خطأ. وإنتاج تلك القيمة هو ما تفعله معاملات المقارنة — وواحد منها في JavaScript لا ينبغي استخدامه إطلاقًا.
| Operator | Asks | Example |
|---|---|---|
| === | Same value AND same type | 5 === 5 is true |
| !== | Not the same | 5 !== "5" is true |
| > < | Greater, less | age > 17 |
| >= <= | Or equal to | score >= 50 |
| == | Same after converting types | Do not use this |
The last row is the one to learn and then avoid. == converts the two sides before comparing, which produces results almost nobody wants: "" == 0 is true, "0" == false is true, and null == undefined is true. Use === always.
== تحوّل الجانبين قبل المقارنة، ما ينتج نتائج لا يريدها أحد تقريبًا: فـ"" == 0 صح، و"0" == false صح، وnull == undefined صح. استخدم === دائمًا.Comparison produces a value
const isAdult = age >= 18; isAdult now holds true or false
That is a real value you can store, print, and pass around —
not something that only exists inside an if.
if, makes the next few lessons much easier. A well-named boolean is also the clearest thing you can put in a condition. Run the demonstration.
if، تجعل الدروس القليلة التالية أسهل بكثير. والقيمة المنطقية حسنة التسمية أوضح ما تضعه في شرط. شغّل العرض.Naming a boolean
A boolean should be named so that reading it aloud in a condition makes a sentence. isAdult, hasUnpaidInvoice, canEdit, shouldRetry all do. status, check and flag do not — they force the reader to go and find out what true means, and that lookup happens every time anyone reads the line.
isAdult وhasUnpaidInvoice وcanEdit وshouldRetry كلها تفعل. وstatus وcheck وflag لا — فهي تجبر القارئ على البحث عن معنى صح، ويحدث ذلك البحث في كل مرة يقرأ فيها أحد السطر.=== أبدًا، للسبب الذي أعطاه الدرس 5 — فـ0.1 + 0.2 === 0.3 خطأ. وإن وجب أن تقارن كسورًا، فافحص أن الفرق أصغر من مقدار ضئيل بدلًا من ذلك. وهذا ينطبق أيضًا على أي مجموع حسبته: فمقارنة سعر محسوب بقيمة مكتوبة من تلك العيوب التي تعمل شهورًا ثم تفشل في طلب واحد بعينه.
Comparing strings works, and it compares them character by character in a way that is not alphabetical order: "Zebra" < "apple" is true, because capital letters sort before lower case. For sorting names in a way a person would expect — and especially for Arabic — use localeCompare, which knows about language.
"Zebra" < "apple" صح، لأن الحروف الكبيرة تُرتَّب قبل الصغيرة. ولترتيب الأسماء بطريقة يتوقعها إنسان — وخصوصًا للعربية — استخدم localeCompare الذي يعرف اللغة.if (age >= 18 && country === "EG" && !isBanned)، اكتب const canRegister = age >= 18 && country === "EG" && !isBanned; ثم if (canRegister). فالمنطق متطابق والصيغة الثانية تشرح نفسها لمن يقرؤها بعدك.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
Why use === rather than ==?
=== لا ==؟"0" == false and null == undefined are also true. Almost nobody wants those, so use === always.
"0" == false وnull == undefined صحيحتان أيضًا. ولا يريد ذلك أحد تقريبًا، فاستخدم === دائمًا.
2.
What does age >= 18 produce on its own?
age >= 18 وحدها؟Seeing a comparison as an expression that produces a value makes conditions and functions much easier later.
3. Which is a well-named boolean?
Read it in a condition: if (hasUnpaidInvoice) is a sentence. if (flag) sends the reader hunting for what true means.
if (hasUnpaidInvoice) جملة. وif (flag) ترسل القارئ للبحث عن معنى صح.Score / النتيجة: 0 / 3
Your task / مهمتك
Write a page that checks whether someone may register: they must be 18 or over, have agreed to the terms, and not be on a blocked list. Build each condition as a separately named boolean before combining them, and print every intermediate value so the reasoning is visible. Then demonstrate three surprising == results and explain why === avoids them.
اكتب صفحة تفحص هل يجوز لأحد التسجيل: يجب أن يكون 18 فأكثر، وأن يوافق على الشروط، وألا يكون في قائمة محظورة. وابنِ كل شرط قيمةً منطقية مسمّاة منفصلة قبل جمعها، واطبع كل قيمة وسيطة ليكون الاستدلال مرئيًا. ثم اعرض ثلاث نتائج مفاجئة لـ== واشرح لماذا يتجنبها ===.
- Each condition named separately before combining كل شرط مسمّى منفصلًا قبل الجمع
- Every intermediate boolean printed كل قيمة منطقية وسيطة مطبوعة
- Names that read as sentences in a condition أسماء تُقرأ جملًا في شرط
- Three surprising == results, explained ثلاث نتائج مفاجئة لـ== مشروحة