The lodash method `_.includes` exported as a module for checking if a value is found in a collection
npx @tessl/cli install tessl/npm-lodash-includes@4.3.0Lodash Includes is a modular lodash package that exports the _.includes method for checking if a value is found in a collection. It provides a unified interface for searching arrays, objects, strings, and arguments objects using SameValueZero equality comparison.
npm install lodash.includes// ES modules (default export)
import includes from 'lodash.includes';For CommonJS:
const includes = require('lodash.includes');import includes from 'lodash.includes';
// Search in arrays
includes([1, 2, 3], 2);
// => true
// Search in objects (searches values, not keys)
includes({ 'user': 'fred', 'age': 40 }, 'fred');
// => true
// Search in strings (substring search)
includes('pebbles', 'eb');
// => true
// Search with starting index
includes([1, 2, 3], 1, 2);
// => false (starts searching from index 2)Checks if a value is found in a collection using SameValueZero equality comparison.
/**
* Checks if value is in collection. This method supports arrays, objects,
* strings, and arguments objects. Uses SameValueZero for equality comparisons.
*
* @param {Array|Object|string} collection - The collection to inspect
* @param {*} value - The value to search for
* @param {number} [fromIndex=0] - The index to search from
* @param {*} [guard] - Enables use as iteratee for methods like `_.reduce` (internal parameter)
* @returns {boolean} Returns `true` if value is found, else `false`
*/
function includes(collection, value, fromIndex, guard);Supported Collection Types:
Search Behavior:
NaN with NaN-0 with 0indexOffromIndex Parameter:
length + fromIndex)false (except empty string in strings)0Usage Examples:
// Basic array search
includes([1, 2, 3], 1);
// => true
includes([1, 2, 3], 4);
// => false
// Search with fromIndex
includes([1, 2, 3, 1], 1, 2);
// => true (finds second occurrence)
includes([1, 2, 3], 1, 2);
// => false (starts from index 2)
// Negative fromIndex
includes([1, 2, 3], 3, -1);
// => true (starts from last element)
includes([1, 2, 3], 1, -2);
// => false (starts from second-to-last element)
// Object search (searches values)
includes({ 'a': 1, 'b': 2, 'c': 3 }, 2);
// => true
includes({ 'user': 'fred', 'age': 40 }, 'fred');
// => true
// String search (substring)
includes('hello world', 'world');
// => true
includes('hello', 'hi');
// => false
// String with fromIndex
includes('hello world', 'o', 5);
// => true (finds 'o' in 'world')
// Special equality cases
includes([1, NaN, 3], NaN);
// => true (matches NaN)
includes([-0], 0);
// => true (matches -0 with 0)
includes([0], -0);
// => true (matches 0 with -0)
// Empty collections
includes([], 1);
// => false
includes({}, 1);
// => false
includes('', 'a');
// => false
// Edge cases with fromIndex
includes([1, 2, 3], 1, 10);
// => false (fromIndex >= length)
includes([1, 2, 3], 1, -10);
// => true (negative fromIndex clamped to 0)
// As iteratee function
[1, 2, 3].every(x => includes([1, 2, 3, 4, 5], x));
// => true
// Method chaining (if using full lodash)
// _(collection).includes(value)The includes function is designed to handle edge cases gracefully:
falsefalse or starts from beginning/end)baseIndexOf for efficient searchingString.prototype.indexOf for fast substring search