To check if a string ends with a specific suffix in JavaScript, you can use the endsWith()
method of the String class.
In this example,
str1
with a string value of "Hello World"
.endsWith()
method of the string object str1
with the suffix "World"
.endsWith()
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.let str1 = 'Hello World';
let endsWithWorld = str1.endsWith('World');
console.log(`String ends with suffix 'World': ${endsWithWorld}`);
String ends with suffix 'World': true
In this example,
str2
with a string value of "apple"
.endsWith()
method of the string object str2
with the suffix "banana"
.endsWith()
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.let str2 = 'apple';
let endsWithBanana = str2.endsWith('banana');
console.log(`String ends with suffix 'banana': ${endsWithBanana}`);
String ends with suffix 'banana': false
In this tutorial, we learned How to Check if String ends with Specific Suffix in JavaScript language with well detailed examples.