- 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
JavaScript Check whether string1 ends with strings2 or not
We are required to write a JavaScript function that takes in two strings, say, string1 and string2 and determines whether the string1 ends with string2 or not.
For example −
"The game is on" Here, "on" should return true
While,
"the game is off" Above, “of" should return false
Let's write the code for this function −
Example
const first = 'The game is on'; const second = ' on'; const endsWith = (first, second) => { const { length } = second; const { length: l } = first; const sub = first.substr(l - length, length); return sub === second; }; console.log(endsWith(first, second)); console.log(endsWith(first, ' off'));
Output
The output in the console will be −
true false
- Related Articles
- Python - Check whether a string starts and ends with the same character or not
- Check whether a string ends with some other string - JavaScript
- Check whether a number is a Fibonacci number or not JavaScript
- How to check whether a key exist in JavaScript object or not?
- How to check whether a number is finite or not in JavaScript?
- Check whether Enter key is pressed or not and display the result in console with JavaScript?
- How to check whether a NaN is a NaN or not in JavaScript?
- JavaScript: How to check whether an array includes a particular value or not?
- Check whether field exist in MongoDB or not?
- Check whether IdentityHashMap empty or not in Java?
- Set whether the flexible items should wrap or not with JavaScript?
- How to check whether a value is a safe integer or not in JavaScript?
- Check if string ends with desired character in JavaScript
- Check whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript
- Check whether a NavigableMap empty or not in Java

Advertisements