Easy Tutorial
❮ Jsref While Dom Obj Hidden ❯

JavaScript endsWith() Method

JavaScript String Object

Example

Determine if a string ends with a specified substring (case-sensitive):

let str = "Hello world";
str.endsWith("world")   // Returns true
str.endsWith("World")   // Returns false

Definition and Usage

The endsWith() method is used to determine whether a string ends with a specified substring (case-sensitive).

It returns true if the substring is found at the end of the search string, otherwise it returns false.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

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 Values

Parameter Description
searchvalue Required. The substring to search for.
length Sets the length of the string. Default is the original string length 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 substring is found at the end of the search string, otherwise returns false.
JavaScript Version: ECMAScript 6
--- ---

More Examples

Example

Determine with different string lengths:

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

JavaScript String Object

❮ Jsref While Dom Obj Hidden ❯