string.includes()
finds one string inside another
GIVEN searching a single character
WHEN "x" is found THEN then s.includes("x")
returns true
WHEN the searched character is not contained THEN it returns false
GIVEN searching for a string
WHEN the searched string matches exactly THEN true
is returned
WHEN searching for "A" THEN "a" is not found, the search is case-sensitive
GIVEN the searched string is an empty string ""
it is always found
WHEN the searching in an empty string THEN this returns true
WHEN the searched string is NOT empty THEN s.includes("")
returns true
GIVEN we use not only strings
JavaScript tries to coerce (convert) the parameter to a string
WHEN searching for the number 4
THEN it is found in a string that contains it
WHEN searching for an array THEN the array is coerced to a string first
WHEN searching with an object that has a toString()
method THEN the result of that function call is used as search string
some searches you should prevent
WHEN searching for undefined
in a string THEN this returns false
WHEN the search parameter is a regular expression THEN includes()
throws an error
GIVEN a 1st parameter, the position where to start searching from
WHEN given 1
THEN it does not find a
after position 1 in abc
WHEN the position is given as a string THEN it gets coerced to a number
GIVEN an invalid positions parameter, it gets converted to 0
WHEN passing undefined
THEN the search starts at position 0
WHEN given a negative numbers THEN the search starts at position 0
WHEN given NaN
THEN the search starts at position 0
Links
The official specification, actually quite good to read for this function.
The Mozilla Developer Network docs, contains good examples.
Required Knowledge
- basics (Arrow functions)
- `let` declaration (Block scope)
- `const` declaration (Block scope)
- basics (Default parameters)
Related Katas
Template strings
String API
string.includes()
string.repeat(count)
string.startsWith()
string.endsWith()
String.raw
string.trimStart()
string.matchAll()
Difficulty Level
BEGINNER
First Published
14 July 2015
Stats
16 tests to solve