CKTIS / JavaScript
String.prototype.endsWith — checking a string suffix
String.prototype.endsWith checks whether a string ends with specified text and returns true or false. It is case-sensitive, leaves the string unchanged, and lets you specify an earlier end boundary for the check.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
str.endsWith(searchString[, endPosition])String.prototype.endsWith compares searchString with a string's suffix and returns true or false without changing the original. The comparison is case-sensitive and uses UTF-16 code units, without Unicode normalization. This is an ECMAScript standard-library method, not a browser API.
The optional endPosition sets an exclusive end boundary: the method checks the suffix of the prefix ending at that boundary, not a substring starting there. Omitting this argument or passing undefined uses the full length. After conversion to an integer, the value is clamped between 0 and the string's length; negative values do not count backward from the end. Positions are measured in UTF-16 code units.
An empty searchString returns true, even at boundary 0, provided any required conversion of endPosition succeeds. Conversion happens before matching: passing 0n as endPosition throws a TypeError even when searchString is empty. The searchString argument is converted to a string, so omitting it means the text "undefined". An argument recognized as a regular expression causes a TypeError. Calling the method with null or undefined as this also causes a TypeError.
In the example, boundary 10 falls immediately after report.txt, so checking .txt returns true even though the full string ends with .bak. Checking .BAK returns false because letter case differs.
Examples
const filename = "report.txt.bak";
console.log(filename.endsWith(".bak"));
console.log(filename.endsWith(".txt", 10));
console.log(filename.endsWith(".BAK"));
console.log(filename.endsWith("", 0));Result
true true false true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.