Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

endsWith

Determines whether a string ends with a specified string and returns true or false. An optional end position lets you test a portion of the string.

Since
ES2015

How it works

Call this method on a string to test whether the characters before a chosen endpoint match the supplied search string. The endpoint defaults to the string's length, so the ordinary form checks the actual suffix; the optional endpoint lets you test a prefix of the string as though it ended there.

Syntax

endsWith(searchString)
endsWith(searchString, endPosition)

Parameters

NameTypeDescription
searchStringstringThe characters to search for at the end of the string; non-regex values are coerced to strings.
endPositionnumberOptional position where the search string is expected to end; defaults to the string length.

Returns

A boolean: true when the searched characters match at the selected end position, otherwise false.

Key points

  • The method answers a suffix-membership question with a boolean result.
  • The optional second argument chooses where the searched text must finish.
  • Without an endpoint, the comparison reaches the end of the complete string.
  • An empty search string always produces true.
  • Numbers, booleans, and other non-regex values are converted before comparison.
  • The comparison distinguishes letter case.

Common mistakes

  • The search value cannot be a regular expression; passing one throws a TypeError.
  • Leaving out the search argument or passing undefined searches for the literal string "undefined" because non-regex values are converted to strings.
  • Uppercase and lowercase characters must match exactly.

Example: Check a filename extension

const filename = "report.final.pdf";

console.log(filename.endsWith(".pdf")); // true
console.log(filename.endsWith(".pdf", 10)); // false

Checks a filename suffix and demonstrates that a shorter end position changes the portion being tested.

When to use it

Extension and suffix validation

Validate a file or URL suffix before processing it.

Simple text validation

Check whether user-facing text has a required ending without regular expressions.

Bounded substring checks

Test a string segment ending at a known index.

Interview questions

QWhat does String.prototype.endsWith() return?

It returns a boolean indicating whether the string ends with the characters in searchString. The default endpoint is the string length, and an empty search string returns true.

Follow-up: What does the second argument do? It supplies the endpoint used for the comparison, so the method tests the substring ending at that position rather than necessarily the full string.

QHow does endsWith() handle the search argument?

The first argument is converted to a string unless it is a regular expression. A regular expression causes a TypeError instead of being accepted as a pattern.

Follow-up: Why does a regex throw? The method is designed for string matching, and the API explicitly rejects regex arguments.

QHow does endPosition affect the result?

endPosition is the position at which the searched text is expected to finish, with a default of str.length. Values outside the string's range are effectively bounded to the valid range.

Follow-up: What happens when the endpoint is too early for the search string? The method returns false because the requested search text cannot fit before that endpoint.

  • String.prototype.startsWith()
  • String.prototype.includes()
  • String.prototype.indexOf()
  • String.prototype.slice()

Source: String.prototype.endsWith() - JavaScript | MDN. Checked 2026-10-15.