Last Updated : 16 Jul, 2024
The JavaScript str.endsWith() method is used to check whether the given string ends with the characters of the specified string or not
Syntax:str.endsWith(searchString, length)Parameters:
Example 1: Below is an example of the String endsWith() Method Method.
JavaScript
function func() {
// Original string
let str = 'Geeks for Geeks';
// Finding the search string in the
// given string
let value = str.endsWith('for', 9);
console.log(value);
}
func();
Example 2: In this example, the function endsWith() checks for searchString at the end of the given string. Since the searchString is found at the end, therefore this function returns true.
JavaScript
function func() {
// Original string
let str = 'It is a great day.';
// Finding the search string in the
// given string
let value = str.endsWith('day.');
console.log(value);
}
func();
Example 3: In this example, the function endsWith() checks for searchString at the end of the given string. Since the searchString is not found at the end, therefore this function returns false
JavaScript
function func() {
// Original string
let str = 'It is a great day.';
// Finding the search string in the
// given string
let value = str.endsWith('great');
console.log(value);
}
func();
Example 4: In this example, the function endsWith() checks for searchString at the end of the given string. Since the second argument defines the end of the string at index 13 and at this index searchString is found to end, therefore this function returns true.
JavaScript
function func() {
// Original string
let str = 'It is a great day.';
// Finding the search string in the
// given string
let value = str.endsWith('great', 13);
console.log(value);
}
func();
Supported Browsers:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4