
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 if string ends with desired character in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and a single character as the second argument.
The function should determine whether the string specified by the first argument ends with the character specified by the second argument or not. The only condition is that we have to do this without using any ES6 or library methods.
Example
Following is the code −
const str = 'This is a string'; const checkEnding = (str = '', char = '') => { // helper function to grab the last character of the string const getLast = (str = '') => { const { length } = str; return str[length - 1]; }; return getLast(str) === char; }; console.log(checkEnding(str, 'g')) console.log(checkEnding(str, 'h'))
Output
Following is the output on console −
true false
- Related Questions & Answers
- Check if a string ends with given word in PHP
- Check whether a string ends with some other string - JavaScript
- How to check if the string ends with specific substring in Java?
- How to check if a string ends with a specified Suffix string in Golang?
- How to check if string or a substring of string ends with suffix in Python?
- Python - Check whether a string starts and ends with the same character or not
- Check if string begins with punctuation in JavaScript
- JavaScript Check whether string1 ends with strings2 or not
- Find if a string starts and ends with another given string in C++
- Triplet with desired sum in JavaScript
- Count substrings that starts with character X and ends with character Y in C++
- Binary subarrays with desired sum in JavaScript
- How to determine if an input string ends with a specified suffix in JSP?
- Check if a string is sorted in JavaScript
- C# program to check if a string contains any special character
Advertisements