Function includes

  • Checks if a value is present in an array.

    Type Parameters

    • T

      The type of elements in the array.

    Parameters

    • array: T[]

      The array to search.

    • value: T

      The value to search for.

    • fromIndex: number = 0

      The index to start searching from. Default is 0.

    Returns boolean

    true if the value is found in the array, otherwise false.

    // Example 1: Basic usage
    const numbers = [1, 2, 3, 4, 5];
    console.log(includes(numbers, 3)); // Output: true
    // Example 2: Searching from a specific index
    const fruits = ['apple', 'banana', 'cherry', 'date'];
    console.log(includes(fruits, 'banana', 2)); // Output: false
    // Example 3: Using with an empty array
    const emptyArray: number[] = [];
    console.log(includes(emptyArray, 1)); // Output: false