

- 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
JavaScript Check whether string1 ends with strings2 or not
<p>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.</p><p>For example −</p><pre class="result notranslate">"The game is on" Here, "on" should return true</pre><p>While,</p><pre class="result notranslate">"the game is off" Above, “of" should return false</pre><p>Let's write the code for this function −</p><h2>Example</h2><pre class="prettyprint notranslate">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'));</pre><h2>Output</h2><p>The output in the console will be −</p><pre class="result notranslate">true false</pre>
- Related Questions & Answers
- 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
- Check whether IdentityHashMap empty or not in Java?
- Check whether field exist in MongoDB or not?
- How to check whether a number is finite or not in JavaScript?
- How to check whether a key exist in JavaScript object or not?
- Check whether a NavigableMap empty or not in Java
- Set whether the flexible items should wrap or not with 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 if string ends with desired character in JavaScript
- C# Program to check whether a directory exists or not
- Check whether a character is Lowercase or not in Java
Advertisements