Fat arrow function: cú pháp (params) => { statements; return value; }. Có block {}, cần return rõ ràng. Dùng khi logic phức tạp, nhiều dòng.
Ví dụ
Hàm add fat arrow:
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fat Arrow Function</title>
<style>
body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; }
.result { font-size: 20px; font-weight: 500; color: blueviolet; }
</style>
</head>
<body>
<h1>Fat Arrow Function trong JavaScript</h1>
<div class="result"></div>
<button class="Btn">TÍNH TỔNG</button>
<h3>Nhấn nút để gọi add(32, 19) fat arrow</h3>
<script>
const resEle = document.querySelector(".result");
const add = (a, b) => { return a + b; };
document.querySelector(".Btn").addEventListener("click", () => {
resEle.textContent = `Tổng 32 + 19 = ${add(32, 19)}`;
});
</script>
</body>
</html>
Kết quả
Tổng 32 + 19 = 51.
So sánh: Xem bài Fat vs Concise Arrow (ID 1003057897) để hiểu rõ sự khác biệt và khi nào dùng loại nào.