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.
עברית
יותר מפarámeter אחד
- רשמו את הפרמטרים בסוגריים, מופרדים בפסיקים.
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היא פונקציה קצרה; ללא סוגריים היא מחזירה את הביטוי.- פונקציות חיצו (Arrow functions) נוחות כארגומנטים לשיטות על מערכים.
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. · לחץ על הרץ כדי לראות את התוצא כאן.