JavaScript endsWith()
The endsWith()
method in JavaScript is a built-in string method that determines whether a string ends with a specified substring or character. This method returns a boolean value indicating whether or not the string ends with the substring specified.
Syntax
The basic syntax for using the endsWith()
method is as follows:
string.endsWith(searchString, position)
Parameters:
searchString
(required) - Specifies the substring to search for. This parameter is case-sensitive.position
(optional) - Specifies the position in the string at which to end the search. TheendsWith()
method searches the part of the string ending at the position specified. If this parameter is omitted,endsWith()
searches the whole string.
Example
Consider the following example:
const text = 'Hello, World!';
console.log(text.endsWith('!')); // true
console.log(text.endsWith('o')); // false
console.log(text.endsWith('Hello')); // false
console.log(text.endsWith('World', 7)); // true
Output
The output of the above example would be:
true
false
false
true