Compare "ab" with "cd":
let text1 = "ab";
let text2 = "cd";
let result = text1.localeCompare(text2);
Try it Yourself »
let text1 = "cd";
let text2 = "ab";
let result = text1.localeCompare(text2);
Try it Yourself »
More examples below.
The localeCompare()
method compares two strings in the current locale.
The localeCompare()
method returns sort order -1, 1, or 0 (for before, after, or equal).
The current locale is based on the language settings of the browser.
string.localeCompare(
compareString)
Parameter | Description |
compareString | Required. The string to compare with. |
Type | Description |
A number | One of 3 values: -1 if the string is sorted before the compareString 0 if the two strings are equal 1 if the string is sorted after the compareString |
let text1 = "ab";
let text2 = "ab";
let result = text1.localeCompare(text2);
Try it Yourself »
let text1 = "A";
let text2 = "a";
let result = text1.localeCompare(text2);
Try it Yourself »
localeCompare()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!