JavaScript String endsWith()
Examples
Check if a string ends with "world":
let text = "Hello world";
let result = text.endsWith("world");
Try it Yourself »
let text = "Hello World";
let result = text.endsWith("world");
Try it Yourself »
More examples below.
Definition and Usage
The endsWith()
method returns true
if a string ends with a specified string.
Otherwise it returns false
.
The endsWith()
method is case sensitive.
See also the startswith()
method.
Syntax
string.endsWith(searchvalue, length)
Parameters
Parameter | Description |
searchvalue | Required. The string to search for. |
length | Optional. The length of the string to search. Default value is the length of the string. |
Return Value
Type | Description |
A boolean | true if the string ends with the value,
otherwise false . |
More Examples
Check if the 11 first characters of a string ends with "world":
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
Try it Yourself »
Browser Support
endsWith()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
endsWith()
is not supported in Internet Explorer 11 (or earlier).