Lesson 4 / الدرس 4

Types: what kind of value is this / الأنواع: أي نوع من القيم هذا

Every value has a kind, and the kind decides what the operators do. Confusing a number with a string of digits is the single most common beginner bug in JavaScript.

لكل قيمة نوع، والنوع يقرر ما تفعله المعاملات. والخلط بين رقم ونص من الأرقام أشيع عيب مبتدئ في JavaScript.

TypeExampleWhat it is for
number20, 3.5, -1Anything you count or calculate with
string'hello', "20"Text — always in quotes
booleantrue, falseA yes-or-no answer
undefinedundefinedA box that was never filled
nullnullDeliberately empty — you set it
array[1, 2, 3]An ordered list — lesson 15
object{ name: "Sara" }Named values together — lesson 17

The difference between the fourth and fifth rows matters: undefined means nobody put anything here, and null means somebody deliberately put nothing here. One is usually a mistake; the other is a decision.

The + that means two things

2 + 2          ->  4        two numbers: addition
'2' + '2'      ->  '22'     two strings: joining
'2' + 2        ->  '22'     one of each: JavaScript joins
'10' - 2       ->  8        minus has no joining meaning, so it converts

This is why a form field, which always gives you a STRING,
produces 205 when you expected 25.
That last line is the bug, and every JavaScript beginner meets it. A number typed into an input arrives as text, and + joins text. Run the demonstration to see it happen and to see the fix.

Checking and converting

To do thisWrite
Ask what type something istypeof value
Text to a numberNumber(text)
Text to a whole numberparseInt(text, 10)
Anything to textString(value)
Round to two decimals for displayvalue.toFixed(2)
Check a conversion failedNumber.isNaN(result)

toFixed is for showing a value, not for storing one — it returns a string, so the result of price.toFixed(2) cannot be added to anything. Format at the last moment, calculate with numbers.

JavaScript is what is called dynamically typed: a variable can hold a number now and a string later, and nothing stops it. That is convenient and it is why the bug above exists — other languages would have refused to add a string to a number at all. The discipline has to come from you: decide what a variable holds and keep it holding that.

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

Preview / المعاينة

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

1. What does "20" + 5 produce?

2. What is NaN, and why is x === NaN useless?

3. What is the difference between undefined and null?

Your task / مهمتك

Build a small page with two number inputs and a button that shows their sum. Make it wrong first — read the values and add them directly — and show the result. Then fix it with Number() and show that. Finally, type letters into one of the fields and report what happens, then handle that case so the page says something useful instead of NaN.

ابنِ صفحة صغيرة بحقلي أرقام وزر يعرض مجموعهما. واجعلها خاطئة أولًا — اقرأ القيمتين واجمعهما مباشرة — وأظهر النتيجة. ثم أصلحها بـNumber() وأظهر ذلك. وأخيرًا اكتب حروفًا في أحد الحقلين وأبلغ بما يحدث، ثم عالج تلك الحالة لتقول الصفحة شيئًا نافعًا بدل NaN.

  • The wrong version, with its output shown الصيغة الخاطئة ومخرَجها معروض
  • The fixed version beside it الصيغة المصلَحة بجانبها
  • typeof used to prove what each value is typeof مستخدمة لإثبات نوع كل قيمة
  • The NaN case handled with a useful message حالة NaN معالَجة برسالة نافعة
How do you want to submit? / كيف تريد التسليم؟