includes method
The includes
method performs
a case-sensitive search for a given
string within the current string. The
first parameter of the method is the
string to be found, the second
optional parameter is the position
from which to start the search. After
execution, the method returns
true
or false
.
Syntax
string.includes(what to search, [where to start searching]);
Example
Let's check if there is the string
'ab'
in the string 'abcde'
:
let res = 'abcde'.includes('ab');
console.log(res);
The code execution result:
true
Example
Now let's search from the fifth character of the current string:
let res = 'ab cd ef'.includes('cd', 5);
console.log(res);
The code execution result:
false
Example
Let's do a case-sensitive search for a string:
let res = 'abcde'.includes('AB');
console.log(res);
The code execution result:
false
See also
-
the
at
method
that searches a character by its position number in a string -
the
startsWith
method
that checks the start of a string -
the
endsWith
method
that checks the end of a string -
the
indexOf
method
that searches for a substring -
the
lastIndexOf
method
that searches for the last occurrence of a substring