Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript String endsWith() Method

Improve Article
Save Article
Like Article
  • Last Updated : 04 Jan, 2023
Improve Article
Save Article
Like Article

The JavaScript 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)

Parameters: 

  • searchString: The searchString is a string of characters that is to be searched at the end of the given string. 
  • length: The length determines the length of the given string from the beginning to be searched for the search string

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

Example 1: Below is an example of the String endsWith() Method Method.

JavaScript




<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);
        console.log(value);
    }
    func();
</script>


Output:

true

Example 2: 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.

JavaScript




<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.');
        console.log(value);
    }
    func();
</script>


Output:

true

Example 3: 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

JavaScript




<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');
        console.log(value);
    }
    func();
</script>


Output:

false

Example 4: 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

JavaScript




<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);
        console.log(value);
    }
    func();
</script>


Output:

true

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

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
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!