
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Check whether a string ends with some other string - JavaScript
- Check if a string ends with given word in PHP
- How To Check If Cell Begins Or Ends With A Specific Character In Excel?
- Python - Check whether a string starts and ends with the same character or not
- How to check if a string ends with a specified Suffix string in Golang?
- How to check if the string ends with specific substring in Java?
- How to check if string or a substring of string ends with suffix in Python?
- Check if string begins with punctuation in JavaScript
- JavaScript Check whether string1 ends with strings2 or not
- Triplet with desired sum in JavaScript
- Find if a string starts and ends with another given string in C++
- Count substrings that starts with character X and ends with character Y in C++
- Binary subarrays with desired sum in JavaScript
- Find word character in a string with JavaScript RegExp?
- Check if the Slice of bytes ends with specified suffix in Golang

Advertisements