Arrow functions · Hàm mũi tên
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
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{ }.
Tiếng Việt
Hàm mũi tên
- Một cách viết hàm ngắn gọn hơn:
const square = (n) => n * n;. - Với một biểu thức duy nhất sau
=>, giá trị sẽ được trả về tự động — không cầnreturn, không cần{ }.
const square = (n) => n * n;
console.log(square(5)); // → 25
English
More than one parameter
- List the parameters in the brackets, separated by commas.
Tiếng Việt
Nhiều tham số hơn
- Liệt kê các tham số trong ngoặc đơn, phân tách bằng dấu phẩy.
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.
Tiếng Việt
Lưu ý
- Cần thân hàm dài hơn? Sử dụng
{ }và sau đó bạn phải tự viếtreturn. - Hàm mũi tên nổi bật như những trợ thủ ngắn gọn — bạn sẽ truyền chúng cho
.mapvà.filterkhi đến phần phương pháp mảng.
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.
Tiếng Việt
Lỗi thường gặp
x => x + 1là một hàm ngắn gọn; không có ngoặc nhọn thì nó sẽ trả về biểu thức.- Hàm mũi tên hữu ích làm đối số cho các phương pháp mảng.
English
Now you try
- Write each arrow function.
- Press Check answer to test it.
Tiếng Việt
Bây giờ bạn thử
- Viết mỗi hàm mũi tên.
- Nhấn Kiểm tra đáp án để thử nghiệm.
Write an arrow function triple that returns its argument times 3: const triple = (n) => n * 3;. · Viết một hàm mũi tên triple trả về đối số nhân 3: const triple = (n) => n * 3;.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write an arrow function add with two parameters that returns their sum. · Viết một hàm mũi tên add với hai tham số và trả về tổng của chúng.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write an arrow function isEven that returns true when its argument is even (n % 2 === 0). · Viết một hàm mũi tên isEven trả về true khi đối số của nó là số chẵn (n % 2 === 0).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.