English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String localeCompare() Method

 JavaScript String Object

localeCompare()The method compares two strings in the current language environment.

This method returns a number that indicates whether the string is located before, after, or equal to compareString in the sorting order.

Syntax:

string.localeCompare(compareString)
var a = 'AB';
var b = 'CD';
var c = a.localeCompare(b);
Test and see‹/›

Browser Compatibility

All browsers fully support the localeCompare() method:

Method
localeCompare()isisisisis

Parameter Value

ParameterDescription
compareStringThe string compared with the reference string

Technical Details

Return value:a number indicating whether the reference string is in the sorting ordercompareStringbefore, after, orwithIdentical.
return one of the following values:
  • -1if the reference string iscompareStringsorted before

  • if two strings are equal, then 0

  • if the reference string iscompareStringsorted afteris1

JavaScript Version:ECMAScript 1

 JavaScript String Object