Arrow functions · الدوال السهمية
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
English
Arrow functions
- A shorter way to write a function:
const square = (n) => n * n;. - With a single expression after
=>, the value is returned automatically — noreturn, no{ }.
العربية
دالات السهم
- طريقة مختصرة لكتابة الدالة:
const square = (n) => n * n;. - مع تعبير واحد بعد
=>، يتم إرجاع القيمة تلقائياً — لاreturnولا{ }.
const square = (n) => n * n;
console.log(square(5)); // → 25
English
More than one parameter
- List the parameters in the brackets, separated by commas.
العربية
أكثر من معامل
- عرّف المعاملات داخل الأقواس، مفصولة بفاصلة.
const add = (a, b) => a + b;
console.log(add(3, 4)); // → 7
English
Watch out
- Need a longer body? Use
{ }and then you must writereturnyourself. - Arrow functions shine as short helpers — you will hand them to
.mapand.filterwhen we reach array methods.
العربية
انتبه
- تحتاج إلى جسم أطول؟ استخدم
{ }ثم يجب عليك كتابةreturnبنفسك. - تبرز دالات السهم كمساعدات قصيرة — ستقدمها لـ
.mapو.filterعند الوصول إلى طرق المصفوفات.
English
Common mistakes
x => x + 1is a short function; with no braces it returns the expression.- Arrow functions are handy as arguments to array methods.
العربية
أخطاء شائعة
x => x + 1هي دالة قصيرة؛ بدون أقواس مجعدة فإنها تُرجع التعبير.- دالات السهم مفيدة كحجج لطرق المصفوفات.
English
Now you try
- Write each arrow function.
- Press Check answer to test it.
العربية
الآن جرب بنفسك
- اكتب كل دالة سهم.
- اضغط على تحقق من الإجابة لاختبارها.
Write an arrow function triple that returns its argument times 3: const triple = (n) => n * 3;. · اكتب دالة سهمية triple تُرجع معاملها مضروباً في 3: const triple = (n) => n * 3;.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write an arrow function add with two parameters that returns their sum. · اكتب دالة سهمية add بمعاملين تُرجع مجموعهما.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write an arrow function isEven that returns true when its argument is even (n % 2 === 0). · اكتب دالة سهمية isEven تُرجع true إذا كان معاملها زوجياً (n % 2 === 0).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.