Syntax, Parameter and Return Value
Syntax:
_.endsWith([string=''], [target], [position=string.length])
Parameters:
-
[string=''] (string): The string to inspect.
-
[target] (string): The string to search for.
- [position=string.length] (number): The position to search up to.
Return Value:
(boolean) - Returns true if string ends with target, else false.
Examples
Basic Usage for Checking String Ending:
JavaScript
var _ = require('lodash');
var file = 'example.txt';
console.log(_.endsWith(file, '.txt'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Demonstrates checking if a file name ends with a specific file extension.
Validating URL Formats:
JavaScript
var url = 'https://www.example.com/page';
console.log(_.endsWith(url, '/page'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Shows how to check if a URL ends with a specific path.
Specifying Position for Checking:
JavaScript
var text = 'Hello world!';
console.log(_.endsWith(text, 'world', 11));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
An example of using the position parameter to specify where in the string to stop the search.
Categorizing Strings Based on Ending:
JavaScript
var products = ['prod_A', 'prod_B', 'test_A', 'test_B'];
var testProducts = products.filter(p => _.endsWith(p, '_A'));
console.log(testProducts);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['prod_A', 'test_A']
Demonstrates filtering an array of strings based on their endings.
Frequently Asked Questions
How does _.endsWith() differ from String.prototype.endsWith()?
_.endsWith() is Lodash's implementation and is similar to JavaScript's native String.prototype.endsWith(). The primary difference is that Lodash methods like _.endsWith() can be chained with other Lodash utilities, providing consistency in a Lodash-based codebase.
Can _.endsWith() handle case-sensitive checks?
_.endsWith() is case-sensitive. For case-insensitive checks, both the string and target need to be converted to the same case (e.g., using .toLowerCase()).
Is _.endsWith() suitable for checking multiple possible endings?
For multiple endings, _.endsWith() would need to be used in conjunction with other methods, such as Array.prototype.some(), to check each possibility.
Conclusion
Lodash's _.endsWith() method is an effective tool for verifying whether a string ends with a specified substring. It's especially useful in file type validation, URL format checking, and other text analysis tasks where the end of a string is significant.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.