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 لا ينبغي استخدامه إطلاقًا.

OperatorAsksExample
===Same value AND same type5 === 5 is true
!==Not the same5 !== "5" is true
> <Greater, lessage > 17
>= <=Or equal toscore >= 50
==Same after converting typesDo 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.

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.
Seeing a comparison as an expression that produces a value, rather than as part of an 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.

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.

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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Why use === rather than ==?

2. What does age >= 18 produce on its own?

3. Which is a well-named boolean?

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 ثلاث نتائج مفاجئة لـ== مشروحة
How do you want to submit? / كيف تريد التسليم؟