JavaScript String localeCompare Explained With Examples
String localeCompare() sorts text the way people expect. See locales, numeric and sensitivity options, accent handling and when Intl.Collator is better.

Sort a list of names with the default sort() and "Zoe" lands before "adam", and "éric" lands after "zoe". Nothing is broken: the default sort compares UTF-16 code units, and every uppercase letter has a smaller code unit than every lowercase one, while accented letters sit far above z. People do not read an index that way. localeCompare() is the built-in answer, since it compares two strings by the rules of a language, and it is the right argument for sort() whenever the strings are text a person will read. This post covers the return value, the locale and options arguments, and the cases where the result differs from what you expect. Everything runs on Node.js 24, which ships with full ICU data, so the locale rules below hold without extra setup. For the plain-substring neighbors, see [the post on indexOf()](/library/javascript-string-indexof).
What localeCompare() takes and returns
The signature is reference.localeCompare(compareString, locales, options). It compares the string it is called on with compareString and returns a number.
| Result | Meaning |
|---|---|
| Negative | reference sorts before compareString |
| Zero | The two are equal under the chosen rules |
| Positive | reference sorts after compareString |
| Argument | Meaning | Default |
|---|---|---|
compareString | The string to compare against | none |
locales | A BCP 47 tag such as "en" or "sv", or an array of them | The runtime's default locale |
options | An object with sensitivity, numeric, caseFirst, ignorePunctuation, usage and others | {} |
The specification promises only the sign of the result, not its size. Current V8 returns -1, 0 or 1, and code that relies on that exact magnitude is relying on an implementation detail. The examples below print Math.sign() of the result through a small helper so the output states only what is guaranteed.
const cmp = (a, b, locales, options) =>
Math.sign(a.localeCompare(b, locales, options));
console.log(cmp("a", "b"));
console.log(cmp("b", "a"));
console.log(cmp("a", "a"));-1
1
0Sorting with it
The method has the shape of a comparator, so it plugs straight into Array.prototype.sort().
const names = ["b", "a", "C", "B"];
console.log([...names].sort());
console.log([...names].sort((x, y) => x.localeCompare(y)));[ 'B', 'C', 'a', 'b' ]
[ 'a', 'b', 'B', 'C' ]The default sort puts both uppercase letters first, because B and C have smaller code units than a. The locale-aware sort groups the letters, a then b then C, and breaks the tie between b and B in favor of lowercase first, which is the default tie-break for English. We copy the array with spread because sort() changes the array in place.
The locale decides the order
The same two strings can sort differently depending on the language, because languages disagree about where accented letters go.
console.log(cmp("ä", "z", "de"));
console.log(cmp("ä", "z", "sv"));-1
1In German, ä sorts together with a, so it comes before z. In Swedish, ä is a separate letter placed after z. If you leave locales out, the result follows the machine's locale, and a test that passes on a developer laptop can fail on a server with a different default. For output that must be the same everywhere, pass a locale explicitly. Passing a tag the runtime cannot parse throws.
try {
"a".localeCompare("b", "not_a_locale");
} catch (err) {
console.log(err.name);
}RangeErrorNumeric ordering
A string sort puts "file10" before "file2", since 1 comes before 2. The numeric option compares runs of digits by their value.
const files = ["file10", "file2", "file1"];
console.log([...files].sort((a, b) => a.localeCompare(b)));
console.log(
[...files].sort((a, b) => a.localeCompare(b, undefined, { numeric: true })),
);[ 'file1', 'file10', 'file2' ]
[ 'file1', 'file2', 'file10' ]Passing undefined for locales keeps the default locale while still letting you supply options. The option handles non-negative integers inside strings. It does not understand signs, decimal points or exponents, so "1.5" and "1.10" compare their digit runs separately and give a result you may not want for version numbers or decimals.
Sensitivity: what counts as different
The sensitivity option sets which differences matter. There are four levels, and they decide whether the result is zero.
sensitivity | a vs A | a vs á | a vs b |
|---|---|---|---|
"base" | equal | equal | different |
"accent" | equal | different | different |
"case" | different | equal | different |
"variant" | different | different | different |
const base = { sensitivity: "base" };
const accent = { sensitivity: "accent" };
console.log(cmp("a", "A", undefined, base));
console.log(cmp("a", "á", undefined, base));
console.log(cmp("a", "A", undefined, accent));
console.log(cmp("a", "á", undefined, accent));0
0
0
-1A search box that should match cafe against café uses "base". A sort that keeps accent differences but ignores case uses "accent". The default, "variant" for sorting, treats every difference as significant. Because the comparison returns zero rather than a boolean, an equality check reads a.localeCompare(b, undefined, { sensitivity: "base" }) === 0.
Canonical equivalence and punctuation
Unlike ===, the comparison treats text that is canonically the same as equal. The letter é written as one code point and as e plus a combining accent are different strings that collate identically.
const composed = "é";
const decomposed = "é";
console.log(composed === decomposed);
console.log(cmp(composed, decomposed));
console.log(cmp("a-b", "ab", undefined, { ignorePunctuation: true }));false
0
0That makes it useful as an equality test for text that came from different sources. If you want the same effect for includes() or indexOf(), which compare code units, [normalize()](/library/javascript-string-normalize) is the tool. The ignorePunctuation option skips punctuation and, depending on the locale, spaces while comparing.
A mistake we hit: the hand-written comparator
The first mistake is skipping localeCompare() and writing the comparison by hand.
const words = ["é", "e", "z", "a"];
console.log([...words].sort());
console.log([...words].sort((a, b) => a.localeCompare(b, "en")));
console.log([...words].sort((a, b) => (a > b ? 1 : -1)));[ 'a', 'e', 'z', 'é' ]
[ 'a', 'e', 'é', 'z' ]
[ 'a', 'e', 'z', 'é' ]The first and third lines match because > compares code units, exactly as the default sort does. The third also never returns zero, which breaks the comparator contract for equal elements: an equal pair gets -1 in both directions, so the result depends on the engine's sorting algorithm. Only the second line is what a reader of English text expects.
The second mistake is testing the exact value.
const order = "a".localeCompare("b");
console.log(order < 0);
console.log(order === -1);true
trueBoth print true in Node.js 24, which is why the habit survives. The specification only promises the sign, so we write < 0, === 0 and > 0, and we never compare against -1 or 1.
Reusing a collator for large sorts
Each call to localeCompare() with a locale or options has to resolve the locale and collation rules, which the method is allowed to do again on every call. The Intl.Collator object does that setup once, and its compare property is a bound function you can hand to sort().
const collator = new Intl.Collator("en", { numeric: true });
const files = ["file10", "file2", "file1"];
console.log(files.sort(collator.compare));[ 'file1', 'file2', 'file10' ]For a handful of items, the difference does not matter and localeCompare() reads better. For sorting thousands of strings, create the collator once and reuse it. The mechanism is the repeated setup, which a collator avoids; we have not measured it here, and you should measure on your own data if it matters.
Choosing between localeCompare and its neighbors
| You want | Use |
|---|---|
| Sort text for a reader | localeCompare() or Intl.Collator |
| Sort many strings repeatedly | Intl.Collator created once |
| Sort technical keys the same on every machine | default sort() or < |
| Accent-insensitive equality | localeCompare() with sensitivity: "base" |
| Find a substring ignoring case | toLowerCase() then [includes()](/library/javascript-string-includes) |
We keep the default code-unit sort for identifiers, file hashes and anything compared by machines, because its stability across environments is the point. We use locale-aware comparison for names, titles and labels.
Where localeCompare stops being the right tool
It compares whole strings, so it cannot tell you whether one string contains another. It also depends on data the runtime ships. A build of Node.js with reduced ICU data supports fewer locales and falls back to a default, and a browser may differ from Node.js in the finer points of a collation. Do not use it where the order has to be identical across runtimes, such as a canonical form for signing or a sort key stored in a database. For those, sort by code point or by a normalized key you define.
It is also the wrong tool for natural-language ranking, fuzzy matching or search relevance. It answers "which comes first", not "how similar". For similarity you need an edit distance or a search index, which this method does not provide.
The method is defined in the ECMAScript specification as String.prototype.localeCompare, with the locale rules in the ECMA-402 Intl specification. MDN documents it on the String.prototype.localeCompare page and the reusable object on the Intl.Collator page.


