HomeToolsAbout a20k

includes

What is it

Returns boolean indicating whether an array includes a certain value among its entries

const items = [1,2,3,4,5]; const includesTwo = items.includes(7); console.log(includesTwo); // false const array1 = ["element1", "element2"]; array1.includes("element2"); // true

Additional Examples

const pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // true console.log(pets.includes('at')); // false // additional examples [1, 2, 3].includes(2) // true [1, 2, 3].includes(4) // false [1, 2, 3].includes(3, 3) // false [1, 2, 3].includes(3, -1) // true [1, 2, NaN].includes(NaN) // true ["1", "2", "3"].includes(3) // false

Example: check if guest is on the guest list

const guestList = ["guest1", "guest2"]; let guestName = prompt("What is your name?"); if (guestList.includes(guestName)) { alert("Welcome"); } else { alert("Sorry you are not on the list"); }
© VincentVanKoh