The Wayback Machine - https://web.archive.org/web/20241112005533/https://www.geeksforgeeks.org/javascript-string-endswith-method/
Open In App

JavaScript String endsWith() Method

Last Updated : 16 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The JavaScript str.endsWith() method 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
function func() {

    // Original string
    let str = 'Geeks for Geeks';

    // Finding the search string in the
    // given string
    let value = str.endsWith('for', 9);
    console.log(value);
}
func();

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
function func() {

    // Original string
    let str = 'It is a great day.';

    // Finding the search string in the
    // given string
    let value = str.endsWith('day.');
    console.log(value);
}
func();

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
function func() {

    // Original string
    let str = 'It is a great day.';

    // Finding the search string in the
    // given string
    let value = str.endsWith('great');
    console.log(value);
}

func();

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
function func() {

    // Original string
    let str = 'It is a great day.';

    // Finding the search string in the
    // given string
    let value = str.endsWith('great', 13);
    console.log(value);
}
func();

Output
true

Supported Browsers: 

  • Chrome 41
  • Edge 12
  • Firefox 17
  • Opera 28
  • Safari 9

JavaScript String endsWith() Method – FAQs

What does the endsWith() method do in JavaScript?

The endsWith() method determines whether a string ends with the characters of a specified substring. It returns true if the string ends with the specified characters, and false otherwise.

Can endsWith() be case-sensitive?

Yes, the endsWith() method is case-sensitive. It differentiates between uppercase and lowercase characters, so “Hello”.endsWith(“hello”) will return false.

What happens if the optional length parameter is used in endsWith()?

When the length parameter is provided, the method considers the string up to that length for checking the specified substring. It acts as if the string were truncated to that length.

How does endsWith() handle empty strings?

If searchString is an empty string, endsWith() will return true for any string because every string ends with an empty substring.

Can endsWith() be used with non-string data types?

The endsWith() method is specifically designed for strings. If used on non-string data types, the method will first convert them to strings before performing the operation.

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



Previous Article
Next Article

Similar Reads

TypeScript String endsWith() Method
The endsWith() method in TypeScript checks if a string ends with a specified substring. It accepts the substring to search for and an optional position parameter to end the search at a specific index, returning a boolean result. Syntaxstring.endsWith(searchString: string, position?: number): boolean;ParametersearchString: The string you want to see
2 min read
What is endsWith() Method in JavaScript ?
In JavaScript, the endsWith() method is used to determine whether a string ends with a specified suffix (another string). It returns true if the string ends with the specified suffix, and false otherwise. Syntax:string.endsWith(suffix, length)string: The string to be checked.suffix: The string to be searched for at the end of string.length (optiona
2 min read
Lodash _.endsWith() Method
Lodash _.endsWith() method is used to check whether the string ends with the given target string or not. Syntax:_.endsWith([string=''], [target], [position=string.length]);Parameters: string: This parameter holds the string that needs to be inspected.target: This parameter holds the string that needs to be searched.position: This parameter holds th
1 min read
PHP | startsWith() and endsWith() Functions
startsWith() Function The StartsWith() function is used to test whether a string begins with the given string or not. This function is case insensitive and it returns boolean value. This function can be used with Filter function to search the data. Syntax bool startsWith( string, startString ) Parameters: This function accepts two parameters as men
3 min read
Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts
Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a" - Shift 3 times - "d" Character "b" - Shift 2 ti
3 min read
Javascript Program to Check if a string can be obtained by rotating another string d places
Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An appro
4 min read
JavaScript Program to Return Original String of Compressed String
The compressed string follows a format where each character is followed by its count. we need to print a string having the same number of characters present in the compressed string. Below are the approaches to return original string of compressed string using Example: Input: compressedString= "a3b2c1"Output: originalString="aaabbc"These are the fo
2 min read
Smallest Window in a String Containing all the Characters of Another String using JavaScript
Given two strings, "str" (the larger string) and "pattern" (the smaller string), the task is to find the smallest window in "str" that contains all the characters of "pattern" in any order using JavaScript. Examples: Input: str = "ADOBECODEBANC"pattern = "ABC"Output: BANCInput: str = "this is a test string"pattern = "tist";Output: t striTable of Co
3 min read
JavaScript Insert a string at position X of another string
Given two strings, the task is to insert one string in another at a specified position using JavaScript. We're going to discuss a few methods, these are: Methods to Insert String at a Certain IndexTable of Content Method 1: Using JavaScript String slice() and Array join() MethodsMethod 2: JavaScript String substr() Method Method 3: Using JavaScript
4 min read
How to check a given string is an anagram of another string in JavaScript ?
In this article, we will learn how to check a given string is an anagram of another string in JavaScript. Before that, we should learn what is an anagram. An anagram is a word or sentence, which usually contains all the original letters exactly once, in order to arrange the letters of a different term or phrase. Some of the examples are given below
3 min read
How to check if a string "StartsWith" another string in JavaScript ?
In JavaScript, the 'startsWith()' method is a built-in method that allows one to check whether a string starts with a specified substring. Syntax: It takes a single argument, which is the substring we want to check whether the string starts with. string.startsWith(searchString[, position])Approach 1: Using startWith methodThe 'searchString' paramet
2 min read
Check if a Given String is Binary String or Not in JavaScript
Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string. Example: Input: "101010"Output: True, binary stringInput: "110211"Output: Falee,Explanati
4 min read
Difference between String.slice and String.substring in JavaScript
These 2 functions are quite similar in their Syntax But are different in some cases. Let's see the difference between them. JavaScript slice() Method:This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted part. The first character starts with index 0. Syntax
3 min read
Difference Between String.prototype.replace() and String.prototype.replaceAll() in JavaScript
The String.prototype.replace and String.prototype.replaceAll are frequently used to the modify strings. Understanding the differences between these methods is crucial for the effective string handling. This article explores the characteristics, applications and examples of both the replace and replaceAll methods. These are the following topics that
3 min read
How to count string occurrence in string using JavaScript ?
In JavaScript, we can count the string occurrence in a string by counting the number of times the string is present in the string. This can be done in the following ways: Table of Content Approach 1: Using match() functionApproach 2: Using a loopApproach 3: Using split() functionApproach 4: Using Indexof()Approach 5: Using regular expressionsApproa
4 min read
Check if a String is Valid JSON String using JavaScript
In JavaScript, JSON (JavaScript Object Notation) is a format used for representing structured data as text. It's widely used for data exchange between web servers and browsers. Sometimes, we need to check if a given string is a valid JSON before using it in our code. If the string follows the correct JSON format, we return true, otherwise, we retur
5 min read
Javascript Program to Check if a string can be obtained by rotating another string 2 places
Given two strings, the task is to find if a string can be obtained by rotating another string in two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwise Asked in: Amazon Interview 1- There can be only two cases: a) Clockwi
2 min read
TypeScript String String.fromCodePoint() Method
The fromCodePoint() is an inbuilt TypeScript String method. It is mainly used to get the string value of any given Unicode point value. This method is really helpful when you want to handle characters that are not readily available through keyboard input. Syntax:String.fromCodePoint(...codePoints: number[]): stringParameters:...codePoints: An array
1 min read
String includes() Method in JavaScript
JavaScript str.includes() function is used to check if the argument string is present in the given string or not. This function is case-sensitive. The syntax of this function is as follows: str.includes(searchString, position) Arguments The first argument to this function searchString is the string to be searched in the given string. The second arg
2 min read
How to define a new method for calculating the actual length of string in JavaScript using prototype ?
In JavaScript, you can use the String. length property to get the length of a string variable. This method will give you a string that is enclosed in quotes no matter what it is. We will get one issue while using this String. length method. Let's discuss when we will get the issue with that String.length method and how to solve it. Syntax: string.l
3 min read
JavaScript method to get the URL without query string
The task is to get the URL name of the page without using a query string with the help of JavaScript. replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue) Parameters: searchVal: This parameter is required. I
3 min read
JavaScript String trimEnd() and trimRight() Method
This article demonstrates trimEnd and trimRight methods of JavaScript string. Both methods are used to perform the same task and their implementation is shown below: JavaScript trimEnd() methodThe trimEnd() method in JavaScript is used to remove white space from the end of a string. The value of the string is not modified in any manner, including a
2 min read
JavaScript String trimStart() and trimLeft() Method
This article demonstrates trimEnd and trimRight methods of JavaScript string. Both methods are used to perform the same task and their implementation is shown below: JavaScript trimStart() method:The trimStart() method in JavaScript is used to remove whitespace from the beginning of a string. The value of the string is not modified in any manner, i
2 min read
String blink() Method in Javascript
In Javascript, the blink() method is a string method that is used to display a string inside the blink tag. Syntax: string.blink() Note: This method has been DEPRECATED and is no longer recommended. Parameter Values: This method does not accept any parameters. Return Values: It returns a string value with a blink tag. JavaScript Version: JavaScript
1 min read
Javascript String @@iterator Method
String [@@iterator]( ) Method is used to make String iterable. [@@iterator]() returns an iterator object which iterates over all code points of String. String[@@iterator] is a Built-in Property of String. We can use this method by making a string iterator. We can make an iterator by calling the @@iterator property of String. In place of @@iterator,
2 min read
Which built-in method returns the calling string value converted to upper case in JavaScript ?
The toUpperCase() built-in method returns the calling string value converted to uppercase in the javascript. This method does not affect any of the special characters, digits, and alphabets that are already in the upper case. Syntax: String.prototype.toUpperCase() Approach: This is a simple HTML file. In the body, there are two p tags one with id t
2 min read
Which built-in method combines two strings and returns a new string in JavaScript ?
In this article, we will know how to combine the text of two strings and return the new string. There are two best-fit methods for this situation: JavaScript concat() methodJavaScript join() methodWe will explore both these methods & their implementations in detail & understand them through the examples. Using concat() method: The concat()
2 min read
Implement polyfill for String.prototype.trim() method in JavaScript
In this article, we will learn how to implement a polyfill for a string.prototype.trim() method in JavaScript. A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or that the new version of the browser does not have th
3 min read
JavaScript String isWellFormed() Method
JavaScript String.isWellFormed() method is used to check if the string contains a lone surrogate or not. A lone surrogate is a string that contains Unicode characters in the range 0xD800–0xDBFF and 0xDC00–0xDFFF. It returns trues if the lone surrogate is not present. If the string is not well-formed it will give an error if we use the encodeURI() m
1 min read
What is the use of the String.trim() Method in JavaScript ?
The String.trim() method in JavaScript is used to remove whitespace (spaces, tabs, and newline characters) from both ends of a string. It returns a new string with the leading and trailing whitespace removed. Example: Here, the original string has extra spaces at the beginning and end. The trim() method is called on that string, resulting in a new
1 min read
three90RightbarBannerImg