Ignoring result from pure array method¶
ID: js/ignore-array-result
Kind: problem
Security severity:
Severity: warning
Precision: high
Tags:
- maintainability
- correctness
Query suites:
- javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
The concat
, join
and slice
methods are pure and do not modify any of the inputs or the array the method is called on. It is therefore generally an error to ignore the return value from a call to one of these methods.
Recommendation¶
Use the returned value from the calls to concat
, join
or slice
.
Example¶
A function extend
is defined in the following example. The function uses the concat
method to add elements to the arr
array. However, the extend
function has no effect as the return value from concat
is ignored:
var arr = [1,2,3];
function extend(others) {
arr.concat(others);
}
Assigning the returned value from the call to concat
to the arr
variable fixes the error:
var arr = [1,2,3];
function extend(others) {
arr = arr.concat(others);
}
References¶
Mozilla Developer Network: Array concat.
Mozilla Developer Network: Array slice.
Mozilla Developer Network: Array join.