0

This works:

var animals = ["caterpillar", "dog", "bird"];
var catMatch = /cat/i;
var catFound = animals.some(function(animalName) {
  return catMatch.test(animalName);
});

console.log(catFound);

But this doesn't

var animals = ["caterpillar", "dog", "bird"];
var catMatch = /cat/i;
var catFound = animals.some(catMatch.test);

console.log(catFound);

Why doesn't the second version work?

Ry-
  • 218,210
  • 55
  • 464
  • 476
linuxdan
  • 4,476
  • 4
  • 30
  • 41

1 Answers1

2

RegExp.prototype.test depends on its this value to work. You can pass the required this value to some:

var catFound = animals.some(catMatch.test, catMatch);
Ry-
  • 218,210
  • 55
  • 464
  • 476