Lesson 10 / الدرس 10

Arrow functions / الدوال السهمية

A shorter way to write a function, used constantly in modern code. The syntax has four forms worth recognising, and one behaviour that is not just shorthand.

طريقة أقصر لكتابة دالة، تُستخدم باستمرار في الشيفرة الحديثة. وللصيغة أربعة أشكال تستحق التعرف، وسلوك واحد ليس مجرد اختصار.

Four forms of the same thing

function (a, b) { return a + b; }      the long form
(a, b) => { return a + b; }            an arrow, with a body
(a, b) => a + b                        one expression: returned automatically
a => a * 2                             one parameter: brackets optional
() => 'hello'                          no parameters: brackets required

x => ({ id: x })                       returning an object needs the ( )
The last line is the one that catches people: without the brackets, JavaScript reads the braces as a function body rather than an object, and the function silently returns nothing. Run the demonstration.

When to use which

SituationPrefer
A small function passed to filter, map, sortA short arrow
A callback of one lineA short arrow
A named function you will call by namefunction, or an arrow with a name
Something twenty lines longEither — but give it a name
A method on an object that uses thisfunction, not an arrow

The last row is the behaviour difference rather than a style preference. An arrow does not get its own this, which is usually exactly what you want in a callback and occasionally exactly what you do not want on an object method.

That this difference is worth one honest paragraph rather than a whole lesson. In an ordinary function, this depends on how the function was called, which is a famous source of confusion. An arrow function does not have its own — it uses whatever this meant where it was written. In practice: inside a listener, an arrow keeps the surrounding this while a plain function gets the element. If you avoid this entirely, which beginners can and should, none of this affects you yet.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What does p => { p.price * 2 } return?

2. Why does x => ({ id: x }) need the brackets?

3. When is a plain function the better choice?

Your task / مهمتك

Take five functions you wrote in the basics course and rewrite each as an arrow, choosing the shortest form that stays readable. Show both versions side by side. Then deliberately write the braces mistake in a map, print the result, and explain why a list of undefined appears with no error. Finish by naming one of your five that you would leave as a plain function, and why.

خذ خمس دوال كتبتها في دورة الأساسيات وأعد كتابة كل واحدة سهمًا، مختارًا أقصر شكل يبقى مقروءًا. وأظهر الصيغتين جنبًا إلى جنب. ثم اكتب خطأ الأقواس المعقوفة عمدًا في map، واطبع النتيجة، واشرح لماذا تظهر قائمة من undefined بلا خطأ. واختم بتسمية واحدة من الخمس ستتركها دالة عادية، ولماذا.

  • Five functions, both forms shown خمس دوال بالصيغتين
  • At least one of each of the four arrow forms واحدة على الأقل من كل شكل من الأربعة
  • The braces mistake shown producing undefined خطأ الأقواس معروضًا وهو ينتج undefined
  • One left as a plain function, with a reason واحدة متروكة دالة عادية مع سبب
How do you want to submit? / كيف تريد التسليم؟