xxxxxxxxxx
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango"); // true
var n = fruits.includes("Django"); // false
xxxxxxxxxx
const ratings = [1,2,3,4,5];
let result = ratings.includes(4);
console.log(result); // true
result = ratings.includes(6);
console.log(result); // false
xxxxxxxxxx
var arr = ["name", "id"];
if (arr.indexOf("name") > -1) console.log('yeah');
else console.log('nah');
xxxxxxxxxx
function checkInput(input, words) {
return words.some(word => input.toLowerCase().includes(word.toLowerCase()));
}
console.log(checkInput('"Definitely," he said in a matter-of-fact tone.',
["matter", "definitely"]));
xxxxxxxxxx
Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE:
console.log(['joe', 'jane', 'mary'].includes('jane')); //true
xxxxxxxxxx
const ratings = [1,2,3,4,5];
let result = ratings.includes(4);
console.log(result); // true
result = ratings.includes(6);
console.log(result); // false
xxxxxxxxxx
let isInArray = arr.includes(valueToFind[, fromIndex])
// arr - array we're inspecting
// valueToFind - value we're looking for
// fromIndex - index from which the seach will start (defaults to 0 if left out)
// isInArray - boolean value which tells us if arr contains valueToFind
xxxxxxxxxx
function checkInput(input, words) {
return words.some(word => new RegExp(word, "i").test(input));
}
console.log(checkInput('"Definitely," he said in a matter-of-fact tone.',
["matter", "definitely"]));