jskatas.org Continuously Learn JavaScript. Your Way.

String API: string.includes()

Finds string within another string.

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

string.includes() determines if a string can be found inside another one

finding a single character

  • can be done (a character is also a string, in JS)
  • reports false if character was not found

find a string

  • that matches exactly

search for an empty string, is always true

  • in an empty string
  • in abc

special/corner cases

  • search for undefined in a string fails
  • searches are case-sensitive
  • must NOT be a regular expression

coerces the searched "thing" into a string

  • e.g. from a number
  • e.g. from an array
  • e.g. from an object, with a toString() method

takes a position from where to start searching

  • does not find a after position 1 in abc
  • even the position gets coerced

invalid positions get converted to 0

  • e.g. undefined
  • negative numbers
  • NaN

Links

The official specification, actually quite good to read for this function.
The Mozilla Developer Network docs, contains good examples.