Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

تحويل أنواع البيانات - الجزء الثاني

جزء من قسم الأساسيات في رحلة JavaScript على Coddy. الدرس 31 من 77.

تحويل Boolean

// التحويل إلى Boolean
console.log(Boolean(1));      // المخرجات: true
console.log(Boolean(0));      // المخرجات: false
console.log(Boolean(""));     // المخرجات: false
console.log(Boolean("hello")); // المخرجات: true

تحويل String

// التحويل إلى String
let num = 42;
let bool = true;
let str1 = String(num);   // يحول الرقم إلى سلسلة نصية
let str2 = String(bool);  // يحول القيمة المنطقية إلى سلسلة نصية

console.log(str1);  // المخرجات: "42"
console.log(str2);  // المخرجات: "true"
challenge icon

التحدي

سهل

في هذا التحدي، ستتدرّب على التحويل بين أنواع البيانات المختلفة في JavaScript، مع التركيز على تحويلات Boolean وString.

لديك عدة متغيرات بقيم مختلفة. مهمتك هي استبدال كل عنصر نائب null في الكود المبدئي باستدعاء التحويل الصحيح:

  1. حوّل السلسلة النصية '42' إلى boolean باستخدام Boolean()
  2. حوّل الرقم 0 إلى boolean باستخدام Boolean()
  3. حوّل الرقم 7 إلى boolean باستخدام Boolean()
  4. حوّل السلسلة النصية الفارغة '' إلى boolean باستخدام Boolean()
  5. حوّل boolean true إلى string باستخدام String()
  6. حوّل الرقم 123 إلى string باستخدام String()

على سبيل المثال، لتحويل stringValue إلى boolean، استبدل null بـ Boolean(stringValue) بحيث يصبح السطر:
console.log("Boolean('42'): ", Boolean(stringValue));

كل null هو مجرد عنصر نائب. وليس مفهومًا تحتاج إلى تعلّمه. مهمتك هي استبدال كل null باستدعاء التحويل المناسب باستخدام المتغير المُعطى.

جرّب بنفسك

// قيم للتحويل
const stringValue = '42';
const zeroNumber = 0;
const positiveNumber = 7;
const emptyString = '';
const boolValue = true;
const numValue = 123;

// TODO: استبدل null بتحويل Boolean() الصحيح لـ stringValue
console.log("Boolean('42'): ", null);

// TODO: استبدل null بتحويل Boolean() الصحيح لـ zeroNumber
console.log("Boolean(0): ", null);

// TODO: استبدل null بتحويل Boolean() الصحيح لـ positiveNumber
console.log("Boolean(7): ", null);

// TODO: استبدل null بتحويل Boolean() الصحيح لـ emptyString
console.log("Boolean(''): ", null);

// TODO: استبدل null بتحويل String() الصحيح لـ boolValue
console.log("String(true): ", null);

// TODO: استبدل null بتحويل String() الصحيح لـ numValue
console.log("String(123): ", null);
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس الأساسيات

تدرّب بنفسك: مترجم JavaScript عبر الإنترنت