Why 5 is missing ?
console.log(5 === "5")
indexOf(5)
isn’t -1
because 5
and "5"
are two different value ( indexOf uses strict equality ) MDN indexOf
console.log([5].indexOf(5))
console.log(["5"].indexOf(5))
How can i match both
5
or"5"
?
Check for both numeric and string value.
var myArray = ["2", "3", 5]
var found = [];
var range = 10;
for (var i = 1; i < range; i++) {
if (myArray.indexOf(i) === -1 && myArray.indexOf(String(i)) === -1) {
found.push(i);
}
}
console.log(found)
solved JavaScript IndexOf with heterogeneous array [closed]