When a function is called, it executes its block of code and uses a return
statement within the function to specify the value that the
function will produce as its result. This returned value can then be used or assigned to a variable in the calling code.
If a function returns a value that is not used or assigned to a variable, it may indicate that the function is not being used correctly or that
there is a mistake in the code. This can make the code harder to understand and maintain, and can also lead to errors if the return value is needed
later in the code.
Ignoring the return value of a function can be a sign of poor coding practices. It can indicate that the developer did not fully understand the
purpose of the function or did not take the time to properly integrate it into the code.
This rule triggers an issue only on a predefined list of methods from built-in objects (String
, Number
,
Date
, Array
, Math
, and RegExp
) to prevent generating any false-positives.
'hello'.lastIndexOf('e'); // Noncompliant: The return value is lost
Ensure that the return value of the function is used by assigning the return value to a variable.
let lastIndex = 'hello'.lastIndexOf('e');
Or use the value directly as part of an expression.
console.log('hello'.lastIndexOf('e'));