localeCompare (JavaScript)

Compares two strings taking into account the host locale.

Defined in

String (Standard - JavaScript)

Syntax

localeCompare(that:string) : int
Parameters Description
that The comparison string.
Return value Description
int 0 if the two strings are equal.

Usage

String 1 is this object. String 2 is the parameter. If the two strings are equal, the return value is 0. If the strings are not equal, the return value is string 1 minus string 2 for the first character that differs (using the Unicode values of the string).

Examples

(1) This example makes an equal comparison.
var cities = new String("Paris   Moscow   Tokyo");
var string2 = "Paris   Moscow   Tokyo";
if (cities.localeCompare(string2) == 0)
	return cities + " == " + string2;
else
	return cities + "!=" + string2;
(2) This example makes an unequal comparison because of the different number of spaces.
var cities = new String("Paris   Moscow   Tokyo");
var string2 = "Paris Moscow Tokyo";
if (cities.localeCompare(string2) == 0)
	return cities + " == " + string2;
else
	return cities + "!= " + string2;
(3) This example makes an unequal comparison because of the different case.
var cities = new String("Paris   Moscow   Tokyo");
var string2 = "PARIS   MOSCOW   TOKYO";
if (cities.localeCompare(string2) == 0)
	return cities + " == " + string2;
else
	return cities + "!= " + string2;