Returning true if JavaScript array contains an element
问题 So far I have this code: var isMatch = viewedUserLikedUsersArray.indexOf(logged_in_user); if (isMatch >=0){ console.log('is match'); } else { console.log('no match'); } If an element is in an array it will return a number greater or equal to 0 and so I can say isMatch >=0 but this doesn't seem the safest way. How else can I return a true/false from the desired code? 回答1: You could just use Array.prototype.some var isMatch = viewedUserLikedUsersArray.some(function(user){ return user === logged