The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

Syntax

str.endsWith(searchString[, length])

Parameters

searchString
The characters to be searched for at the end of this string.
length
Optional. If provided it is used as the length of str. If omitted, the default value is the length of the string.

Return value

true if the given characters are found at the end of the string; otherwise, false.

Description

This method lets you determine whether or not a string ends with another string. This method is case-sensitive.

Examples

Using endsWith()

var str = 'To be, or not to be, that is the question.';

console.log(str.endsWith('question.')); // true
console.log(str.endsWith('to be'));     // false
console.log(str.endsWith('to be', 19)); // true

Polyfill

This method has been added to the ECMAScript 6 specification and may not be available in all JavaScript implementations yet. However, you can polyfill String.prototype.endsWith() with the following snippet:

if (!String.prototype.endsWith) {
	String.prototype.endsWith = function(search, this_len) {
		if (this_len === undefined || this_len > this.length) {
			this_len = this.length;
		}
		return this.substring(this_len - search.length, this_len) === search;
	};
}

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.endsWith' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'String.prototype.endsWith' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 41Edge Full support YesFirefox Full support 17IE No support NoOpera Full support 28Safari Full support 9WebView Android Full support YesChrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 17Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 4.0.0
Full support 4.0.0
Full support 0.12
Disabled
Disabled From version 0.12: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

See also