Lodash _.endsWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

Checks if string ends with the given target string. An optional position limits how far to search (default: string length). Part of Lodash String (since 3.0.0).

Pass the string and options shown below. Check the return type before you chain another helper.

Syntax

Call _.endsWith. Start with this line.

Lodash
_.endsWith([string], [target], [position])
CallWhat it does
_.endsWith([string], [target], [position])Primary signature.
_.endsWith(string)Transform or inspect a string.

Prefer import endsWith from "lodash/endsWith" when you want a smaller bundle.

Library declarations
Lodash
_.endsWith([string], [target], [position])

Parameters

ParameterDescription
stringOptional. String to inspect; default ''
positionOptional. End index to search up to; default length

Return Value

Return ValueDescription
*true / false

Lodash Examples

Example 1 — Ends With Target

Official docs: "abc" ends with "c".

Lodash
_.endsWith("abc", "c");
// → true
Try It Yourself

How It Works

  1. Default end. Position is the full length (3).
  2. Slice. Last character is "c".
  3. Compare. Matches target → true.
  4. Docs. Matches the first official example.

Example 2 — Does Not End With

Official docs: "abc" does not end with "b".

Lodash
_.endsWith("abc", "b");
// → false
Try It Yourself

How It Works

  1. Last char. The string ends with "c", not "b".
  2. Result. Comparison fails → false.
  3. Note. "b" appears earlier, but that is not the suffix.
  4. Tip. Use position when you want to ignore trailing chars.

Example 3 — Optional position

Treat an earlier index as the end of the string.

Lodash
_.endsWith("abc", "b", 2);
// → true

// inspects "ab" (up to index 2)
Try It Yourself

How It Works

  1. Position. End index is 2 → slice "ab".
  2. Suffix. That slice ends with "b".
  3. Result. true (even though full string ends with "c").
  4. Docs. Matches the third official example.

Example 4 — File Extension Check

A common real-world suffix check.

Lodash
[
  _.endsWith("report.pdf", ".pdf"),
  _.endsWith("report.pdf", ".jpg")
];
// → [true, false]
Try It Yourself

How It Works

  1. Target. Include the dot: ".pdf".
  2. Match. Filename ends with that suffix → true.
  3. Miss. ".jpg" is not the suffix → false.
  4. Tip. Lowercase the name first if extensions may vary in case.

Example 5 — vs startsWith

Prefix vs suffix on the same string.

Lodash
[
  _.endsWith("abc", "c"),
  _.startsWith("abc", "a"),
  _.endsWith("abc", "a"),
  _.startsWith("abc", "c")
];
// → [true, true, false, false]
Try It Yourself

How It Works

  1. endsWith. Looks at the end → "c" matches.
  2. startsWith. Looks at the start → "a" matches.
  3. Cross. Start is not end (and vice versa).
  4. Pair. Use both for full boundary checks.

Browser & Runtime Support

_.endsWith is a Lodash String helper (since 3.0.0). It needs no special browser APIs beyond loading Lodash. Native String.prototype.endsWith is also widely available in modern engines.

✓ Stable · Lodash 3.0.0+

_.endsWith()

Available in [email protected] full builds and the lodash/endsWith modular import. Works in evergreen browsers and Node / Bun / Deno when Lodash is installed.

100% With Lodash 3.0.0+
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 Lodash 3.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import endsWith from "lodash/endsWith" for smaller builds.

Frequently Asked Questions

_.endsWith([string=''], [target], [position=string.length]) returns true if string ends with target. Official docs: _.endsWith('abc', 'c') → true; _.endsWith('abc', 'b') → false; _.endsWith('abc', 'b', 2) → true.
position is the end index to search up to (default string.length). Only the slice ending at that position is checked — so _.endsWith('abc', 'b', 2) looks at 'ab'.
Yes. 'Abc' does not end with 'c' if the last character is 'C'. Normalize with _.toLower first when needed.
endsWith checks the suffix; startsWith checks the prefix. Both accept an optional position.
Same idea as String.prototype.endsWith, including the optional end position. Lodash also coerces values via toString.
Use import endsWith from "lodash/endsWith" or require("lodash/endsWith") for tree-shaking.

Next: _.escape()

Convert & < > " ' to HTML entities.

_.escape() tutorial →

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.

7 people found this page helpful