Javascript - String endsWith() Method

The endsWith() method determines whether a string ends with a specified string.

Description

The endsWith() method determines whether a string ends with a specified string.

This method returns true if the string ends with the characters, and false if not.

The endsWith() method is case sensitive.

Syntax

string.endsWith(searchvalue,length)

Parameter Values

Parameter Require Description
searchvalue Required. The string to search for
length Optional. Specify the length of the string to search. If omitted, the default value is the length of the string

Return

A Boolean. Returns true if the string ends with the value, otherwise it returns false

Check if a string ends with "world", assuming the string is 11 characters long:

Demo

var str = "Hello world, this is a test.";
var n = str.endsWith("world", 11);
console.log(n);

Result

Check if a string ends with "universe.":

Demo

//check where if the string ends with the specified value.
var str = "Hello world, welcome to the universe.";
var n = str.endsWith("universe.");
console.log(n);/*from   w ww  .  j  a  va2  s  .com*/

Result