JavaScript Endswith() Function

JavaScript offers numerous predefined string methods for executing different tasks on strings. One particularly handy string method is endsWith, enabling you to verify if a provided string concludes with a particular sequence of characters. This tutorial delves into the endsWith function in JavaScript, illustrating how to incorporate it effectively within your code.

Syntax:

Here is the syntax of the endsWith function:

Example

string.endsWith(searchString, length)

searchString: (Required)

The string you wish to locate at the conclusion of the initial string.

length (optional):

The parameter determines the size of the string under consideration. If not provided, the search extends to the complete string.

Return value:

The endsWith method provides a boolean outcome, indicating whether the given string concludes with a particular substring - returning true if it does, and false if it does not.

Examples:

Listed below are a few instances that showcase the utilization of the endsWith method:

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

Example

const str = "Hello, world!";

console.log(str.endsWith("!"));

console.log(str.endsWith("world")); 

console.log(str.endsWith("world", 13));

Output:

Output

true

false

false

Explanation:

In the provided illustration, the initial console.log function verifies if the string concludes with an exclamation mark. Subsequently, the following statement confirms if the string concludes with the term "world". Lastly, the third statement restricts the assessment to the initial 13 characters of the string and examines if it concludes with the term "world".

Example 2:

An additional illustration involves checking if a string concludes with a particular character:

Example

const str = "Hello, world!";

if (str.endsWith("!")) {

console.log("The string ends with an exclamation mark");

}

if (str.endsWith("world!")) {

console.log("The string ends with 'world!'");

} else {

console.log("The string doesn't end with 'world!'");

}

Output:

Output

The string ends with an exclamation mark

The string ends with 'world!'

Explanation:

In this instance, the initial if condition verifies if the string concludes with an exclamation mark. Upon such condition, a notification is displayed on the console. Subsequently, the succeeding if condition evaluates if the string concludes with the specific characters "world!". In the event that the string indeed concludes with "world!", a console message is generated affirming the string's ending with "world!". Conversely, if the string does not conclude with "world!", a console message indicating that the string lacks such ending is displayed.

Example 3:

Validate user input

Example

const userInput = prompt("Enter a URL:");

if (userInput.endsWith(".com")) {

console.log("Valid URL: ends with '.com'");

} else {

console.log("Invalid URL: must end with '.com'");

}

Output:

Output

Enter a URL: https://logic-practice.com/

Invalid URL: must end with '.com'

In this instance, the prompt method is utilized to gather input from the user. Subsequently, an if statement is employed to verify if the user input concludes with the characters ".com". If this condition is met, a message confirming the validity of the URL is displayed. Alternatively, if the input does not end with ".com", a message indicating that the URL is invalid and must conclude with ".com" is shown.

Example 4:

Filter an array of strings based on a specific condition

Example

const words = ["apple", "banana", "orange", "grape"];

const filteredWords = words.filter((word) => {

  return word.endsWith("e");

});

console.log(filteredWords);

Output:

Output

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

In the provided illustration, the filter function is employed to generate a fresh array named filteredWords. filter requires a callback function that is applied to every item in the array. This callback function verifies if the ongoing array element concludes with the character "e" by leveraging the endsWith method. Should the condition be met, the element gets appended to the newly created array. Subsequently, the console.log command displays the filteredWords array in the console, comprising solely of words finishing with the character "e".

Conclusion:

The endsWith method in JavaScript is a valuable string operation that lets you verify if a particular string concludes with a specific sequence of characters. This function can be applied for diverse purposes like validating user entries, sorting an array of strings according to a particular criterion, and so on. Understanding the implementation of the endsWith function can greatly assist in crafting productive and optimized JavaScript code.

Input Required

This code uses input(). Please provide values below: