Javatpoint Logo
Javatpoint Logo

JavaScript Endswith() Function

JavaScript provides a lot of built-in string functions to perform various operations on strings. One such useful string function is endsWith(), which allows you to check if a given string ends with a specific set of characters. In this article, we will explore the endsWith() function in JavaScript and see how to use it in your code.

Syntax:

Here is the syntax of the endsWith() function:

searchString: (Required)

The string that you want to search for at the end of the original string.

length (optional):

It specifies the length of the string to be considered. If omitted, the entire string is searched.

Return value:

The endsWith() function returns a boolean value, which is true if the original string ends with the specified string, and false otherwise.

Examples:

Here are some examples that demonstrate how to use the endsWith() function:

Example 1: Check if a string ends with a specific character

Output:

true
false
false

Explanation:

In this example, the first console.log() statement checks whether the string ends with an exclamation mark. The second statement checks whether the string ends with the word "world". The third statement limits the search to the first 13 characters of the string and checks whether it ends with the word "world".

Example 2:

Another example on if a string ends with a specific character:

Output:

The string ends with an exclamation mark
The string ends with 'world!'

Explanation:

In this example, the first if statement checks if the string ends with an exclamation mark. If it does, it will log a message to the console. The second if statement checks if the string ends with the characters "world!". Since the string does end with "world!", it will log a message to the console saying that the string ends with "world!". If the string did not end with "world!", it would log a message saying that the string doesn't end with "world!".

Example 3:

Validate user input

Output:

Enter a URL: https://www.javatpoint.com/
Invalid URL: must end with '.com'

In this example, the prompt() function is used to get user input. The if statement then checks if the user input ends with the string ".com". If it does, it logs a message saying that the URL is valid. Otherwise, it logs a message saying that the URL is invalid and must end with ".com".

Example 4:

Filter an array of strings based on a specific condition

Output:

[ 'apple', 'orange', 'grape' ]

In this example, the filter() method is used to create a new array called filteredWords. The filter() method takes a callback function that is executed for each element in the array. The callback function checks if the current element in the array ends with the letter "e" using the endsWith() function. If it does, the element is added to the new array. Finally, the console.log() statement outputs the filteredWords array to the console, which contains only the words that end with the letter "e".

Conclusion:

The endsWith() function in JavaScript is a useful string function that allows you to check if a given string ends with a specific set of characters. You can use it to perform various tasks, such as validating user input, filtering an array of strings based on a specific condition, and more. Knowing how to use the endsWith() function can be helpful in writing efficient and effective JavaScript code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA