Javascript String endsWith()

Last Updated On Saturday 9th Apr 2022

The endsWith() method returns true if a string ends with the specified string. If not, the method returns false.

Syntax

	str.endsWith(searchString)
str.endsWith(searchString, length)
	

Parameters

  • searchString – The characters to be searched for at the end of str.
  • length ( Optional ) – If provided, it is used as the length of str. Defaults to str.length.

Return value

The endswith() method returns

  • true – if the given characters are found at the end of the string.
  • false – if given characters are not found at the end of the string.

Example 1: Using endsWith() Method

	const str = 'Hello Marsh';
console.log(str.endsWith('Marsh'));  // true
console.log(str.endsWith('new')); // false
	

Output

	true
false
	

The endsWith() method returns true as the sentence ends with Marsh. The method returns false as the sentence doesn’t end with new.

Example 2: endsWith() Method ( Case Sensitive )

  • The endsWith() method is case sensitive.
	const str = 'Hello Marsh';
console.log(str.endsWith('marsh'));  // false
	

Output

	false
	

The endsWith() method is case sensitive, it treats marsh and Marsh as two different strings.