Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript String endsWith() Method

  • Last Updated : 05 Oct, 2021

Below is the example of the String endsWith() Method Method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example:




    <script>
    // JavaScript to illustrate endsWith() function
    function func() {
      
        // Original string
        var str = 'Geeks for Geeks';
      
        // Finding the search string in the
        // given string 
        var value = str.endsWith('for',9);  
        document.write(value);
    }
    func();
    </script>
  • Output:
    true
    

str.endsWith() function is used to check whether the given string ends with the characters of the specified string or not.

Syntax:

str.endsWith(searchString, length)

Arguments:
The first argument to this function is a string of characters searchString which is to be searched at the end of the given string. The second argument to the function is length which determines the length of the given string from the beginning to be searched for the searchString.



Return value:
This function returns the Boolean value true if the searchString is found else it returns the Boolean value false

Examples for the above function are provided below:
Example 1:

var str = 'It is a great day.'
print(str.endsWith('day.'));

Output:

true

In this example the function endsWith() checks for searchString at the end of the given string. Since the searchString is found at the end, therefore this function returns true.

Example 2:

var str = 'It is a great day.'
print(str.endsWith('great'));

Output:

false

In this example the function endsWith() checks for searchString at the end of the given string. Since the searchString is not found at the end, therefore this function returns false.

Example 3:



var str = 'It is a great day.'
print(str.endsWith('great',13));

Output:

true

In this example the function endsWith() checks for searchString at the end of the given string. Since the second argument defines the end of the string at index 13 and at this index searchString is found to end, therefore this function returns true.

Codes for the above function are provided below:

Program 1:




<script>
// JavaScript to illustrate endsWith() function
function func() {
  
    // Original string
    var str = 'It is a great day.';
  
    // Finding the search string in the
    // given string 
    var value = str.endsWith('day.');  
    document.write(value);
}
func();
</script>
print(value); 

Output:

true

Program 2:




// JavaScript to illustrate endsWith() function
<script>
function func() {
  
    // Original string
    var str = 'It is a great day.';
  
    // Finding the search string in the
    // given string 
    var value = str.endsWith('great');
    document.write(value); 
}
func();
</script>

Output:

false

Program 3:




<script>
// JavaScript to illustrate endsWith() function
function func() {
  
    // Original string
    var str = 'It is a great day.';
  
    // Finding the search string in the 
    // given string 
    var value = str.endsWith('great',13);
    document.write(value);  
}
func();
</script> 

Output:

true

Supported Browsers:

  • chrome 41 and above
  • Edge 12 and above
  • Firefox 17 and above
  • Opera 28 and above
  • Safari 9 and above



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!