The in
operator used on an array is valid but the code will likely not have the expected behavior. The in
operator deals
with the indexes of the array, not with the values.
If checking for an array slot is indeed desired, using hasOwnProperty
makes the code intention clearer.
Noncompliant Code Example
function func1() {
let arr = ["a", "b", "c"];
let expectedValue = "b";
if (expectedValue in arr) { // Noncompliant, will be always false
return expectedValue + " found in the array";
} else {
return expectedValue + " not found";
}
}
function func2() {
let arr = ["a", "b", "c"];
let expectedValue = "1"; // index #1 is corresponding to the value "b"
if (expectedValue in arr) { // Noncompliant, will be always true because the array is made of 3 elements and the #1 is always there whatever its value
return expectedValue + " found in the array";
} else {
return expectedValue + " not found";
}
}
Compliant Solution
function func() {
let arr = ["a", "b", "c"];
let expectedValue = "b";
if (arr.includes(expectedValue)) {
return expectedValue + " was found in the array";
} else {
return expectedValue + " not found";
}
}