Quick JavaScript Tip: the some() method

I recently learned of a great JavaScript method I’d never used. It was brought to my attention by Wes Bos. The some() method is a convenient way to test the values of an array and see if at least one value returns true. some() was implemented before ES6 and its support is wide-spread (Internet Explorer 11 supports it).
It’s important to note that this method only works on true Arrays. Other iterable types do not implement this method.
Problem: You have an array containing the types of animals your hotel allows. How can you determine if your pet snake, Mr. Slithers, can come along on your trip?
An Older Way:
var animals = [‘dogs’, ‘cats’, ‘snakes’, ‘birds’, ‘pandas’];
var snakesAllowed = false;

for (var i = 0; i < animals.length; i++) { if(animals[i] === 'snakes') { snakesAllowed = true; break; } } console.log(snakesAllowed); // true Using .some(): // Using arrow functions const animals = ['dogs', 'cats', 'snakes', 'birds', 'pandas']; const snakesAllowed = animals.some((type) => type === ‘snakes’);

console.log(snakesAllowed); // true

// Using normal functions
const animals = [‘dogs’, ‘cats’, ‘snakes’, ‘birds’, ‘pandas’];

const snakesAllowed = animals.some(function(type) {
return type === ‘snakes’;
});

console.log(snakesAllowed); // true

Further reading: MDN web docs

Link: https://dev.to/mattsparks/quick-javascript-tip-the-some-method-207j