المسارات / JavaScript / الدرس 10

typeof null بترجّع object

مبتدئ5 دقايقالدرس 10 من 150تمرين واحد
قضية

typeof null بترجّع 'object'، ففحص null بـtypeof مش شغال، والفحص الصح x === null.

المثال

ده سلوك من 1995 اتثبّت عشان التوافق: أول تطبيق كان بيميّز النوع بعلامة في القيمة، وnull طلعت بنفس العلامة. اتصلّحش لأن التصليح بيكسر مواقع قديمة.

typeof null رجّعت 'object'، والمقارنة الصريحة رجّعت true، وArray.isArray رجّعت false، وطريقة toString كشفت [object Null].

الحالة اللي بتعضّ

لو كتبت typeof value === 'object' كفحص لـobject، هتعدّي مع null وتوقع على value.prop بـTypeError. لازم تضيف value !== null.

الفحص الصح

x === null لو عايز null بس، وx == null لو عايز null وundefined مع بعض.

مكان الاستثناء ده في الجدول.

جدول الأنواع

ليه `==` بتعدّي مع null وundefined.

المقارنة

البرهان

شُغّل على Node v26.14.0، والناتج منسوخ من التشغيل نفسه.

js
console.log('typeof null:', typeof null);
console.log('null === null:', null === null);
console.log('Array.isArray(null):', Array.isArray(null));
console.log('Object.prototype.toString.call(null):', Object.prototype.toString.call(null));
console.log('null == undefined:', null == undefined);
النتيجة
typeof null: object
null === null: true
Array.isArray(null): false
Object.prototype.toString.call(null): [object Null]
null == undefined: true
console.log('typeof null:', typeof null);
console.log('null === null:', null === null);
console.log('Array.isArray(null):', Array.isArray(null));
console.log('Object.prototype.toString.call(null):', Object.prototype.toString.call(null));
console.log('null == undefined:', null == undefined);

التمرين

تمرين 01

افحص قيمة null بثلاث طرق واطبع نتيجة كل واحدة.

جرّب بنفسك قبل ما تروح للدرس اللي بعده.

∎ تمّ البرهان.

اتجرّب على Node v26.14.0 · آخر تشغيل

ابعت الصفحة لحد
واتسابتليجرامX
برهان

الدروس

اتحلّ 0 من 174 تمرين