- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether a string ends with some other string - JavaScript
We are required to write a JavaScript function that takes in two strings say, str1 and str2. The function should determine whether or not str1 ends with str2. Our function should return a boolean on this basis.
Here’s our 1st string −
const str1 = 'this is just an example';
Here’s our 2nd string −
const str2 = 'ample';
Example
Following is the code −
const str1 = 'this is just an example'; const str2 = 'ample'; const endsWith = (str1, str2) => { const { length } = str2; const { length: l } = str1; const sub = str1.substr(l - length, length); return sub === str2; }; console.log(endsWith(str1, 'temple')); console.log(endsWith(str1, str2));
Output
This will produce the following output in console −
false true
- Related Articles
- Check if string ends with desired character in JavaScript
- Python - Check whether a string starts and ends with the same character or not
- How to check whether a string ends with one from a list of suffixes in Python?
- JavaScript Check whether string1 ends with strings2 or not
- How to check if a string ends with a specified Suffix string in Golang?
- Check if a string ends with given word in PHP
- How to check if string or a substring of string ends with suffix in Python?
- Program to check whether a string is subsequence of other in C++
- How to check whether a string contains a substring in JavaScript?
- How to check if the string ends with specific substring in Java?
- How to check whether a string starts with XYZ in Python?
- Find if a string starts and ends with another given string in C++
- Check whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript
- Check if string begins with punctuation in JavaScript
- Check whether a String has only unicode digits in Java

Advertisements