0
0
JavascriptHow-ToBeginner · 3 min read

How to Use includes() for Search in JavaScript

Use the includes() method in JavaScript to check if a string or array contains a specific value. It returns true if found, otherwise false. For example, 'hello'.includes('ell') returns true.
📐

Syntax

The includes() method is used with strings or arrays to check if a value exists inside them.

  • For strings: string.includes(searchString, position)
  • For arrays: array.includes(searchElement, fromIndex)

Parameters:

  • searchString / searchElement: The value to search for.
  • position / fromIndex (optional): The position to start searching from (default is 0).

Returns: true if the value is found, otherwise false.

javascript
const str = 'hello world';
const resultStr = str.includes('world', 0); // true

const arr = [1, 2, 3, 4];
const resultArr = arr.includes(3, 1); // true
💻

Example

This example shows how to use includes() to search for a substring in a string and an element in an array.

javascript
const sentence = 'The quick brown fox jumps over the lazy dog';
const wordToFind = 'fox';
const hasWord = sentence.includes(wordToFind);

const numbers = [10, 20, 30, 40, 50];
const numberToFind = 25;
const hasNumber = numbers.includes(numberToFind);

console.log(`Does the sentence include '${wordToFind}'?`, hasWord);
console.log(`Does the array include ${numberToFind}?`, hasNumber);
Output
Does the sentence include 'fox'? true Does the array include 25? false
⚠️

Common Pitfalls

Some common mistakes when using includes() are:

  • Using includes() on objects or unsupported types (it only works on strings and arrays).
  • Not considering case sensitivity in strings (search is case-sensitive).
  • For arrays, expecting partial matches (it checks for exact elements only).

Example of a common mistake and the correct way:

javascript
// Wrong: searching substring in array elements
const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.includes('app')); // false because 'app' is not an exact element

// Right: use string includes on each element
const hasApp = fruits.some(fruit => fruit.toLowerCase().includes('app'));
console.log(hasApp); // true
Output
false true
📊

Quick Reference

UsageDescriptionExampleReturns
String.includes(searchString, position)Checks if substring exists in string'hello'.includes('ell')true
Array.includes(searchElement, fromIndex)Checks if element exists in array[1,2,3].includes(2)true
Case sensitivitySearch is case-sensitive for strings'Hello'.includes('h')false
Exact match in arraysArray search matches exact elements only['cat','dog'].includes('do')false

Key Takeaways

Use includes() to check if a string or array contains a value, returning true or false.
includes() is case-sensitive when used with strings.
For arrays, includes() checks for exact element matches, not partial matches.
To search substrings inside array elements, combine includes() with array methods like some().
includes() starts searching from an optional position parameter, defaulting to 0.