JavaScript Endswith() Function18 Mar 2025 | 3 min read 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 characterOutput: 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 inputOutput: Enter a URL: https://www.TpointTech.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 conditionOutput: [ '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. Next TopicHow to draw a line using javascript |
We would have heard and learned the recursion concept when approaching different programming languages. In JavaScript also, we have the concept of Recursion, where we make use of the recursive functions. So, in this section, we are going to learn about Recursion and will also see...
6 min read
Introduction: Take into account that you have two Javascript arrays, each with a number of elements. You want to compare the arrays right now. In order to compare two arrays, you must determine whether they have the same number of elements and whether all of those...
3 min read
is a DOM property of JavaScript that allows for styling the CSS (Cascading Style Sheet) classes of an element. is a read-only property that returns the names of the CSS classes. It is a property of JavaScript with respect to the other properties...
5 min read
The onbeforeunload Event happens just before a document is unloaded. It displays a statement in a confirmation dialogue box to stay current page or go to the page using the provided link. You may ask the user whether they wish to stay on the...
4 min read
An Http cookie, also known as a web cookie, is a small piece of data that the server sends to the web browser. The browser may store the cookies and send them back to the server with other requests. The typical use of the cookies...
10 min read
When a user tries to load a page but it is unsuccessful, the page remains unloaded, and a JavaScript onunload event is triggered. The possibility exists that the onunload event will also happen if the browsers have been closed while the page is loaded. When a...
3 min read
The javascript removes the single and multiple classes using a simple method. We can use the query selector with the remove() method. The remove method requires the classList keyword to get the multiple classes. The multiple classes use the "for" loop in the javascript. Syntaxes The syntax...
5 min read
In this article, we will explain about the javaScript and the ripple effect. After that, we will learn the various practical examples of ripple effects with the help of JavaScript. What do you mean by JavaScript? JavaScript is a client-side high-level, interpreted programming and scripting language used...
6 min read
JS First Class Function JavaScript is a dynamic scripting language that allows us to use it in the form of an object-oriented style or functional style. In the same way, one such feature of JavaScript is the First class function. In this section, we will discuss the...
4 min read
? JavaScript is a light-weight object-oriented programming language that is used by several websites for scripting the webpages. It is an interpreted, full-fledged programming language. JavaScript enables dynamic interactivity on websites when it is applied to an HTML document. JavaScript helps the users to build modern web...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India