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.
المقارنةالبرهان
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);التمرين
افحص قيمة null بثلاث طرق واطبع نتيجة كل واحدة.
جرّب بنفسك قبل ما تروح للدرس اللي بعده.
اتجرّب على Node v26.14.0 · آخر تشغيل