By the end of this tutorial, you’ll reliably test whether strings end with a target suffix using Lodash’s _.endsWith() method.
01
Core Syntax
Call _.endsWith(string, target, position).
02
Boolean Result
Returns true or false—never mutates.
03
File Extensions
Validate .pdf, .png, and upload rules.
04
Position Arg
Search only up to an index with the optional third parameter.
05
vs startsWith
Pair with _.startsWith for prefix/suffix checks.
06
Production Tips
Prefer case normalization when extensions may vary in case.
Fundamentals
What Is _.endsWith()?
_.endsWith() checks whether a string ends with a given target substring. It returns a boolean—true when the target matches the tail of the string (or the slice up to position), and false otherwise. It mirrors the native String.prototype.endsWith with Lodash’s consistent API and edge-case handling.
💡
Beginner tip
Think of _.endsWith(fileName, '.pdf') as “is this a PDF file?” without writing manual slice logic.
Suffix checks appear in upload validators, static asset routers, autocomplete filters, and anywhere you branch logic based on how a path or filename ends.
Foundation
📝 Syntax
Provide the haystack string, the suffix to test, and an optional search position:
Reach for Lodash when you want a consistent functional style across utilities; native is fine in modern runtimes.
Compare
📋 _.endsWith vs related operations
Topic
_.endsWith
_.startsWith
_.includes
Native
Matches
Suffix
Prefix
Anywhere
Suffix (endsWith)
Return type
boolean
boolean
boolean
boolean
Position arg
Yes (end index)
Yes (start index)
No
Yes
Typical use
Extensions
Protocols
Substring search
Same as Lodash
In Lodash bundle
Importable alone
Importable alone
Importable alone
Built-in
🧠 How _.endsWith() Works
1
Coerce arguments
Lodash converts string and target to strings.
Input
2
Resolve position
Use provided position or default to string length.
Bounds
3
Slice effective end
Consider only characters up to the resolved position.
Slice
4
Compare tail
Match the last target.length characters against target.
Compare
=
📝
Boolean returned
true when the suffix matches; false otherwise. No string mutation.
Important
📝 Notes
_.endsWith() is case-sensitive unless you normalize case first.
Empty target matches any string (same as native behavior).
The optional position lets you test prefixes of the string as if they were the full length.
Pair with _.startsWith() for full prefix/suffix validation.
Returns false for non-matching suffixes without throwing.
Import lodash/endsWith for tree-shaking.
Wrap Up
Conclusion
_.endsWith() is a focused boolean helper for suffix checks: file types, URL endings, and filtered collections. Keep case normalization in mind and use the position parameter when you need partial-string semantics.
Next, secure your output with _.escape() for HTML entity encoding.
Normalize case for extension checks when users vary casing
Use inside filter() for suffix-based lists
Combine with startsWith for protocol + extension rules
Import lodash/endsWith for small bundles
Document allowed extensions in one constants module
❌ Don’t
Rely on endsWith for MIME-type security alone
Forget that .PDF and .pdf differ without toLower
Use endsWith when you need anywhere-in-string search (use includes)
Assume position defaults are obvious to all readers—comment them
Parse complex URLs with only endsWith—use URL API when possible
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.endsWith()
Use these points whenever you test string suffixes in JavaScript.
5
Core concepts
✔01
Boolean
Returns true or false.
Basics
📄02
Extensions
Validate file suffixes.
Pattern
📏03
position
Optional end index.
API
🔄04
startsWith
Check prefixes too.
Related
🔒05
escape
Next: HTML safety.
Next step
❓ Frequently Asked Questions
_.endsWith() returns true when a string ends with the specified target substring, optionally up to a given position index.
Yes. .pdf and .PDF are different. Use _.toLower() on both values if you need case-insensitive matching.
position is the index treated as the end of the string for the check. It defaults to string.length.
_.includes() checks for a substring anywhere. _.endsWith() only matches a suffix at the end (or at position).
Yes. Lodash endsWith mirrors String.prototype.endsWith for typical use cases.
It is fine for quick extension guards, but combine with MIME checks and server-side validation for security.
Did you know?
Lodash _.endsWith() mirrors native String.prototype.endsWith, including the optional position argument—so _.endsWith('abc', 'b', 2) is true while _.endsWith('abc', 'c', 2) is false. Pair with _.startsWith() for full prefix/suffix checks.