Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Basic Usage for Checking String Ending:
4.2.
JavaScript
4.3.
Validating URL Formats:
4.4.
JavaScript
4.5.
Specifying Position for Checking:
4.6.
JavaScript
4.7.
Categorizing Strings Based on Ending:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.endsWith() differ from String.prototype.endsWith()?
5.2.
Can _.endsWith() handle case-sensitive checks?
5.3.
Is _.endsWith() suitable for checking multiple possible endings?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.endsWith() Method

Author Riya Singh
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

In string manipulation and testing, checking whether a string ends with a certain substring is a common task. Lodash's _.endsWith() method offers a straightforward way to perform this check. 

Lodash _.endsWith() Method

This method is particularly useful in scenarios like validating file extensions, categorizing strings based on their endings, or implementing custom search and filter operations.

Why This Function is Used

The _.endsWith() function is used to determine if a string ends with the given target substring. This method is essential in situations where you need to verify the end portion of a string, such as checking file types, verifying URL formats, or performing text analysis that depends on the string's ending characters.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

JavaScript

var _ = require('lodash');

var file = 'example.txt';

console.log(_.endsWith(file, '.txt'));

Output: 

true


Demonstrates checking if a file name ends with a specific file extension.

Validating URL Formats:

  • JavaScript

JavaScript

var url = 'https://www.example.com/page';

console.log(_.endsWith(url, '/page'));

Output:

true


Shows how to check if a URL ends with a specific path.

Specifying Position for Checking:

  • JavaScript

JavaScript

var text = 'Hello world!';

console.log(_.endsWith(text, 'world', 11)); 

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

JavaScript

var products = ['prod_A', 'prod_B', 'test_A', 'test_B'];

var testProducts = products.filter(p => _.endsWith(p, '_A'));

console.log(testProducts);

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.forInRight() Method
Next article
Lodash _.cond() Method
Live masterclass