Variables — let and const · المتغيرات — let و const
A variable is a named box
- A variable stores a value and gives it a name, so you can use it again later.
letmakes a box you can change.constmakes a box that must stay the same (a constant).
المتغير هو صندوق مُسمّى
- متغير يخزن قيمة ويعطيها اسماً، حتى تتمكن من استخدامه لاحقاً.
letيصنع صندوقاً يمكنك تغييره.constيصنع صندوقاً يجب أن يبقى ثابتاً (a constant).
let age = 15; // a value that may change
const name = "Ada"; // a value that won't
console.log(name, age); // → Ada 15
Change a let with a new value
- Assign again —
count = count + 1— to update alet. count++is a handy shortcut for "add 1 tocount".
غيّر let بقيمة جديدة
- أعد assignment —
count = count + 1— لتحديثlet. count++هو اختصار مفيد لـ "أضف 1 إلىcount".
let count = 0;
count = count + 1;
count++;
console.log(count); // → 2
Watch the boxes change · شاهد الصناديق تتغير
A variable is a named box. let can be changed; const stays fixed. · المتغير هو صندوق مُسمّى. let يمكن تغييره؛ const يبقى ثابتاً.
Watch out
- A
constcannot be reassigned:const x = 1; x = 2;throws an error. - Good habit: use
constby default, and reach forletonly when the value truly changes.
انتبه
constلا يمكن إعادة assignment:const x = 1; x = 2;يرمي خطأ.- عادة جيدة: استخدم
constبشكل افتراضي، واستخدمletفقط عندما تتغير القيمة حقاً.
Common mistakes
- Use
constfor values that never change,letfor those that do. - Declare a variable before you use it.
أخطاء شائعة
- استخدم
constللقيم التي لا تتغير أبداً،letللتي تفعل. - أعلن عن متغير قبل استخدامه.
Now you try
- Use
letorconstto store values. - Press Check answer to test it.
الآن جرب بنفسك
- استخدم
letأوconstلتخزين القيم. - اضغط على تحقق من الإجابة لاختبارها.
Store the text Cairo in a const called city, then log city. · خزن النص Cairo في const يُسمّى city، ثم أخرج city.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
let w = 4 and let h = 3 are the width and height. Work out the area (w * h) and log it (it is 12). · let w = 4 و let h = 3 هما العرض والطول. احسب المساحة (w * h) وأخرجها (وهي 12).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Make a let total = 10, add 5 to it, then log total (it is 15). · أنشئ let total = 10، أضف إليها 5، ثم أخرج total (وهي 15).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.