EN
JavaScript - String endsWith() method example
3
points
The method checks if string ends with some text.
// ONLINE-RUNNER:browser;
const text = 'ABC';
console.log(text.endsWith('C')); // true
console.log(text.endsWith('BC')); // true
1. Documentation
Syntax |
|
Typed syntax | String endsWith(searchString: string, endPosition?: number): boolean |
Parameters |
|
Result | Returns true if the given text was found at the end of the string, otherwise returns false . |
Description |
The
|
2. Practical example
Example 1
// ONLINE-RUNNER:browser;
const text = 'Some text ...';
console.log(text.endsWith('...')); // true
console.log(text.endsWith('text ...')); // true
console.log(text.endsWith('Some')); // false
console.log(text.endsWith('Some text')); // false
Example 2 - with optional endPosition
character
// ONLINE-RUNNER:browser;
// endPosition: 4 9
// | |
// v v
const text = 'Some text ...';
console.log(text.endsWith('Some', 4)); // true
console.log(text.endsWith('text', 9)); // true
console.log(text.endsWith('Some', 9)); // false // 'Some text' doesn't end with 'Some'
console.log(text.endsWith('text ...', text.length)); // true