JavaScript String endsWith()
Syntax & Examples
String.endsWith() method
The endsWith() method of the String class in JavaScript determines whether a string ends with the characters of the specified string, searchString. It can also check within a specific portion of the string by providing an endPosition parameter.
Syntax of String.endsWith()
There are 2 variations for the syntax of String.endsWith() method. They are:
endsWith(searchString)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The characters to be searched for at the end of this string. |
This method determines whether a string ends with the characters of the string searchString.
Returns value of type Boolean
.
endsWith(searchString, endPosition)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The characters to be searched for at the end of this string. |
endPosition | optional | The position within the string at which to end the search. Defaults to the string's length. |
This method determines whether a string ends with the characters of the string searchString, considering only the portion of the string up to endPosition.
Returns value of type Boolean
.
✐ Examples
1 Using endsWith() method to check the end of a string
In JavaScript, we can use the endsWith()
method to check if a string ends with a specified substring.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
endsWith()
method with the argument'world!'
to check if the string ends with'world!'
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const result = str.endsWith('world!');
console.log(result);
Output
true
2 Using endsWith() method with a specified end position
In JavaScript, we can use the endsWith()
method to check if a string ends with a specified substring, up to a certain position.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
endsWith()
method with the arguments'Hello'
and5
to check if the substring from the start to position5
ends with'Hello'
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const result = str.endsWith('Hello', 5);
console.log(result);
Output
true
3 Using endsWith() method to check for a false condition
In JavaScript, we can use the endsWith()
method to demonstrate a case where the string does not end with the specified substring.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
endsWith()
method with the argument'Hello'
to check if the string ends with'Hello'
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const result = str.endsWith('Hello');
console.log(result);
Output
false
Summary
In this JavaScript tutorial, we learned about endsWith() method of String: the syntax and few working examples with output and detailed explanation for each example.