JavaScript String endsWith() method

Definition and usage

If the string ends with the specified string,endsWith() The method returns true.

otherwise returns false.

endsWith() The method is case-sensitive.

See also:

startsWith() method

Example

Example 1

Check if the 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

Example 2

Check if the first 11 characters of the string end with "world":

let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);

Try it yourself

Syntax

string.endsWith(searchvalue, length)

Parameter

Parameter Description
searchvalue Required. The string to search for.
length

Optional. The length of the string to search for.

The default value is the length of the string.

Return value

Type Description
Boolean value If the string ends with this value, trueotherwise, false.

Browser support

endsWith() It is an ECMAScript6 (ES6) feature.

All browsers support ES6 (JavaScript 2015):

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Supported Supported Supported Supported Supported

Internet Explorer 11 (and earlier versions) does not support endsWith().

Related Pages

JavaScript String

JavaScript String Methods

JavaScript String Search