var blacklistedSites = <\"\">;//Returns true if the present site is blacklisted, false otherwisefunction isBlacklistedSite(url) console.log(\"Site is \" + url); blacklistedSites.forEach(function(entry) console.log(\"testing \" + entry); if (entry == document.URL) return true; ); return false;console.log(isBlacklistedSite(\"\"));

You are watching:

This outputs:

Site is does isBlacklistedSite() not detect a match?



The factor your password doesn\"t occupational is the your:

return true;effectively does nothing. It simply returns indigenous the forEach function, which happens anyway regardless of there being a match, or not. Her return true; does not return from your isBlacklistedSite() function. Her isBlacklistedSite() function always exits with:

return false;While you can do this utilizing .forEach(), it is a bad choice. The .forEach() an approach always iterates end every member of the array, regardless of any return worth you provide in the forEach function. You would only use that if you were additionally doing part other operation on every aspect of the variety at the same time. Even then, it can be much better to separate out the two different tasks. If girlfriend did usage it, you would have to keep the detection in a variable identified external come the .forEach(function(){....

Use .indexOf() come test an exact match to variety element

If you want to test for exact matches of variety elements, you have the right to use .indexOf() and also test for a worth > -1.

For instance:

var blacklistedSites = <\"\">;function isBlacklistedSite(url) console.log(\"Site is \" + url); return blacklistedSites.indexOf(url) > -1;//Test v a known matching value.console.log(isBlacklistedSite(blacklistedSites<0>));//Test with a well-known failing value.console.log(isBlacklistedSite(\"foo\"));

If you require a more complicated test, you deserve to use .some()

See more: Down Syndrome The Incredibles, Why Is Down Syndrome A Syndrome

var blacklistedSitesRegExes = *www\\.google\\.com\\/.*espv=2/>;function isBlacklistedSite(url) console.log(\"Site is \" + url); return blacklistedSitesRegExes.some(function(regex) regex.lastIndex = 0; //Prevent contamination from prior tests return regex.test(url); );//Test through a known equivalent value.console.log(isBlacklistedSite(\"\"));//Test with a known failing value.console.log(isBlacklistedSite(\"foo\"));

With restricted availability: .includes() (not for manufacturing code)

.includes() does precisely what you desire (return a Boolean true/false for an exact match). However, the is not as generally accessible as .indexOf(). That is recommended no to usage it in manufacturing code. Because that Arrays, it does not add much advantage over .indexOf(url) > -1.

Additional methods

There are many additional methods easily accessible to Arrays which might be supplied to identify that you have a match. What you usage will depend on your particular needs. As always, you must be responsibility of compatibility worries for any technique you select to use. Several of the obtainable methods room (text from MDN):