This is an issue when passing a function reference directly to array iterator methods like map, forEach,
filter, find, some, every, reduce, reduceRight, or
flatMap.
Array iterator methods in JavaScript pass multiple arguments to their callback functions: the current element, the index, and the entire array. When you pass a function reference directly to these methods, the function receives all these arguments, not just the element.
This can cause unexpected behavior in two scenarios:
For example, if you have [1, 2, 3].map(parseInt), the parseInt function receives both the element and the index. Since
parseInt(string, radix) uses the second parameter as the radix, this produces unexpected results: [1, NaN, NaN] instead of
[1, 2, 3].
This issue is particularly problematic when working with external libraries or modules that might change their function signatures in future versions, potentially breaking your code without any obvious indication.
This can lead to subtle bugs that are difficult to debug, especially when:
These bugs can cause incorrect data processing, application crashes, or security vulnerabilities if the unexpected parameters affect business logic or data validation.
Wrap the function reference in an arrow function or anonymous function to explicitly control which parameters are passed. This ensures only the intended arguments reach your function.
const numbers = [1, 2, 3]; const result = numbers.map(parseInt); // Noncompliant
const numbers = [1, 2, 3]; const result = numbers.map(element => parseInt(element));