Pure function là hàm luôn trả về kết quả giống nhau với cùng input và không có side effects — không thay đổi state ngoài, không gọi API, không modify argument truyền vào.
Ví dụ: const add = (a, b) => a + b là pure, còn const push = (arr, val) => { arr.push(val); return arr; } là impure vì modify arr gốc. Pure functions dễ test (không cần mock), dễ debug, hỗ trợ memoization và là nền tảng của functional programming. Trong React, components và reducers nên là pure functions.
A pure function always returns the same result for the same input and has no side effects — it does not change external state, make API calls, or modify passed-in arguments.
Example: const add = (a, b) => a + b is pure, while const push = (arr, val) => { arr.push(val); return arr; } is impure because it modifies the original arr. Pure functions are easy to test (no mocking needed), easy to debug, support memoization, and are the foundation of functional programming. In React, components and reducers should be pure functions.