String endsWith()
In JavaScript, String.endsWith()
method is used to check if the string ends with a specific suffix string or search string.
Syntax
str.endsWith(search, length)
where
str
is a string.search
is a string.length
is an integer, and optional.
endsWith()
method returns true
if the string str
ends with the specified search
string, else it returns false
. If length
is provided, only this length of the string str
is considered for checking.
Examples
1. In the following program, we check if string value in name
ends with search string 'le'
.
let name = 'apple';
let output = name.endsWith('le');
console.log(output);
2. In the following program, we check if string value in name
ends with search string 'nana'
.
let name = 'apple';
let output = name.endsWith('nana');
console.log(output);
3. In the following program, we check if string value in name
ends with search string 'pl'
in the length of 4
.
let name = 'apple';
let output = name.endsWith('pl', 4);
console.log(output);