Files
30-seconds-of-code/snippets/array-includes.md
Angelos Chalaris fa1c6b5755 Tag, lint, build
2017-12-15 13:06:53 +02:00

388 B

Array includes

Use slice() to offset the array/string and indexOf() to check if the value is included. Omit the last argument, fromIndex, to check the whole array/string.

const includes = (collection, val, fromIndex=0) => collection.slice(fromIndex).indexOf(val) != -1;
// includes("30-seconds-of-code", "code") -> true
// includes([1, 2, 3, 4], [1, 2], 1) -> false