JavaScript
Web Development
Programming
JavaScript Functions
Coding Tutorial

endsWith in JavaScript

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

The endsWith() method in JavaScript is a string method that is used to determine whether a specific string ends with the characters of another string, producing a Boolean result — true or false. This method is particularly useful for validating file extensions, language-specific terminologies, or general text-pattern checks in strings.

Syntax of endsWith

Here is the basic syntax for the endsWith() method:

javascript
str.endsWith(searchString[, length])
  • str: The string on which the method is called.
  • searchString: The characters to be searched for at the end of the string.
  • length (optional): Consider str to be of length characters long. Thus, it will check the characters in searchString against the substring of str that consists of the first length characters. The default value is the length of the whole string.

Basic Example

Here is a simple example demonstrating the use of endsWith():

javascript
1const fileName = 'example.pdf';
2
3// Check if the filename ends with '.pdf'
4if (fileName.endsWith('.pdf')) {
5  console.log('The file is a PDF document.');
6} else {
7  console.log('The file is not a PDF document.');
8}

Output:

 
The file is a PDF document.

Using the Length Parameter

The length parameter can modify the behavior of the method by artificially limiting the length of the string:

javascript
1const str = 'Learning JavaScript';
2
3console.log(str.endsWith('Script'));  // true
4console.log(str.endsWith('Script', 16));  // false, as it checks against 'Learning JavaS'

Case-Sensitivity

The endsWith() method is case-sensitive, meaning that the character case must match exactly:

javascript
1const greeting = 'Hello, World';
2
3console.log(greeting.endsWith('world'));  // false
4console.log(greeting.endsWith('World'));  // true

Practical Use Cases

  1. Validating file extensions - As seen in the initial example, it's commonly used to check file types by looking at their extension.
  2. UI strings verification - Ensuring certain strings (error messages, buttons' text) end with specific characters, which may be critical for consistent UI in web applications.
  3. Natural language processing - To determine if sentences or phrases end with specific punctuation or words.

Browser Compatibility

The endsWith() method works in most modern browsers, including Chrome, Firefox, Edge, Safari, and IE (Internet Explorer) 12+. For Node.js users, it is supported in all versions higher than Node 0.12.x.

Summary Table

FeatureDetail
Method TypeString method
Return TypeBoolean
Parameter: searchStringThe string to search for
Parameter: lengthOptional. The length to use for the search
Case SensitiveYes
Common Use CasesFile type validation, UI consistency checks

Conclusion

The endsWith() method in JavaScript is simple yet powerful for string manipulation and verification based on character patterns. This functionality, combined with its ease of use, makes it a valuable asset in various programming contexts, ensuring that developers can efficiently implement features related to string handling. Always remember the method is case-sensitive, and consider usage implications in globalized applications where character cases might vary.

Understanding and utilizing endsWith() can help in creating robust, error-free applications by validating text-based data effectively. Whether working on front-end user interfaces or back-end logic, endsWith() proves to be an indispensable tool in a developer’s arsenal.


Course illustration
Course illustration

All Rights Reserved.