endsWith method
The endsWith method checks
if a string ends with the substring
specified in the first parameter. If
it ends, then it returns true,
and if it doesn't, then false. The
second optional parameter can specify
the string length. In this case, the
check will not be the real end of the
string, but the specified.
Syntax
string.endsWith(what to search, [string length]);
Example
We check if a string ends with a given substring:
let str = 'abcde';
let res = str.endsWith('cde');
console.log(res);
The code execution result:
true
Example
We check if a string ends with a given substring:
let str = 'abcde';
let res = str.endsWith('xxx');
console.log(res);
The code execution result:
false
Example
Let's set the length of the string to be checked:
let str = 'abcde';
let res = str.endsWith('bc', 3);
console.log(res);
The code execution result:
true
See also
-
the
startsWithmethod
that checks the start of a string -
the
indexOfmethod
that searches for the first occurrence of a substring -
the
lastIndexOfmethod
that searches for the last occurrence of a substring -
the
includesmethod
that searches for a string -
the
atmethod
that searches for a string character