JavaScript endsWith() Method
Example
Check whether a string ends with a specified substring (case-sensitive):
let
str
=
"
Hello world
"
;
str
.
endsWith
(
"
world
"
)
//
Returns true
str
.
endsWith
(
"
World
"
)
//
Returns false
Try it »
Definition and Usage
The endsWith() method is used to determine whether the current string ends with the specified substring (case-sensitive).
If the passed substring is at the end of the search string, it returns true; otherwise it returns false.
Browser support
The numbers in the table indicate the first browser version that supports this property.
| Chrome 41 | Edge 12 | Firefox 17 | Safari 9 | Opera 36 |
| Mar 2015 | Jul 2015 | Oct 2012 | Oct 2015 | Mar 2016 |
Syntax
string.endsWith(searchvalue, length)
Parameter value
| Parameters | Description |
|---|---|
| searchvalue | Required. The substring to search for. |
| length | Sets the length of the string. The default value is the length of the original string, string.length. |
Return Value
| Type | Description |
|---|---|
| Boolean | Returns true if the string ends with the specified value, otherwise returns false. |
Technical Details
| Return value: | Boolean. Returns true if the passed substring is at the end of the search string, otherwise returns false. |
|---|---|
| JavaScript version: | ECMAScript 6 |
More examples
Example
Set different string lengths to test:
var
str
=
"
To be, or not to be, that is the question.
"
;
str
.
endsWith
(
"
question.
"
)
;
//
true
str
.
endsWith
(
"
to be
"
)
;
//
false
str
.
endsWith
(
"
to be
"
,
19
)
;
//
true
Try it »
JavaScript String object