JavaScript String endsWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

In JavaScript, String.prototype.endsWith(searchString, endPosition?) returns true if the string ends with the given text, otherwise false. Matching is case-sensitive. Need a prefix check? Use startsWith().

Remember
str.endsWith("pdf")     → true / false
case matters             → "PDF" ≠ "pdf"
endPosition (optional)   → treat that index as the end
endsWith("")             → always true

Syntax

Call endsWith on the string you want to test.

JavaScript
str.endsWith(searchString);
str.endsWith(searchString, endPosition);
CallWhat it does
str.endsWith(searchString)Checks whether the whole string ends with that text.
str.endsWith(searchString, endPosition)Pretends the string ends at endPosition, then checks the suffix.

Built into JavaScript (ES2015). Call endsWith(...) on a string — no import needed. A RegExp argument throws TypeError.

Library declarations
JavaScript
str.endsWith(searchString)
str.endsWith(searchString, endPosition)

Parameters

ParameterDescription
searchStringRequired. The suffix to look for. Must not be a RegExp.
endPositionOptional. Index treated as the end of the string (default: str.length).

Return Value

Return ValueDescription
booleantrue if the suffix matches, otherwise false.

JavaScript Examples

Example 1 — Basic endsWith()

Ask whether a file name ends with a given extension.

JavaScript
const file = "report.pdf";

console.log(file.endsWith(".pdf"));
console.log(file.endsWith(".PDF"));
console.log(file.endsWith("port.pdf"));
Try It Yourself

How It Works

  1. Exact suffix. The string ends with ".pdf" → true.
  2. Case matters. ".PDF" is different → false.
  3. Longer suffix. "port.pdf" also matches the end → true.

Example 2 — Optional End Position

Use endPosition to check a suffix against only the first part of the string.

JavaScript
const text = "JavaScript";

console.log(text.endsWith("Script"));
console.log(text.endsWith("Java", 4));
console.log(text.endsWith("Script", 4));
Try It Yourself

How It Works

  1. Full string. Without a position, the whole word ends with "Script".
  2. Cut at index 4. Only "Java" is checked, so that suffix matches.
  3. Wrong piece. Inside the first four characters there is no "Script".

Example 3 — Case Sensitivity and Empty Suffix

Exact matches only — and the empty-string quirk.

JavaScript
console.log("File.PDF".endsWith("pdf"));
console.log("File.PDF".toLowerCase().endsWith("pdf"));

console.log("Hello".endsWith(""));
console.log("".endsWith(""));
Try It Yourself

How It Works

  1. Case matters. Uppercase PDF does not match lowercase pdf.
  2. Normalize first. Lower the string, then check the lower suffix.
  3. Empty suffix. endsWith("") is always true, even on "".

Browser & Runtime Support

String.prototype.endsWith() is part of ES2015 (ES6). It works in all modern browsers and Node.js versions in common use today.

✓ Baseline · ES2015

String.prototype.endsWith()

Supported in Chrome 41+, Firefox 17+, Safari 9+, Edge 12+, and Node.js 4+. Not available in Internet Explorer without a polyfill.

98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
endsWith() Excellent

Bottom line: Safe for modern web apps and current Node LTS. For IE11, feature-detect or fall back to str.slice(-suffix.length) === suffix.

Frequently Asked Questions

endsWith(searchString, endPosition?) returns true if the string ends with searchString (optionally checked as if the string ended at endPosition), otherwise false. The search is case-sensitive.
Yes. "File.PDF".endsWith("pdf") is false. Normalize with toLowerCase() when case should not matter.
No. If searchString is a RegExp, endsWith() throws TypeError. Use RegExp.test() with a trailing $ for pattern suffixes.
true for any string. An empty search string is treated as a match at the end.
endsWith() checks the end of the string (or up to endPosition). startsWith() checks the beginning (or from a start position).

Next: startsWith()

Learn how to test whether a string begins with a given prefix.

startsWith() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful