String.prototype.localeCompare() returns a number that tells you how two strings order in a given language (same API as MDN String.prototype.localeCompare()). Learn negative / positive / 0 results, locales and options, sensitivity, numeric sorting, when to prefer Intl.Collator, five examples, and try-it labs.
01
Kind
Instance method
02
Returns
number (<0 / 0 / >0)
03
Locales
Optional
04
Options
sensitivity, numeric…
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
Sorting names, product titles, or filenames often needs language rules — not raw character codes. In German, "ä" may sort near "a"; in Swedish it can sort after "z". That is what localeCompare() is for.
It returns a number you can use in Array.prototype.sort: negative means “this string comes first,” positive means “comes later,” and 0 means “same for this comparison.” Engines with Intl.Collator support delegate to that API.
💡
Beginner tip
Do not check for exactly -1 or 1. Use < 0, > 0, or Math.sign() — engines may return other negative or positive integers.
Four facts to remember about String.localeCompare().
Returns
number
<0 / 0 / >0
Exact -1?
no
Use sign only
Best for
sort()
Locale-aware order
Mutates
no
Strings stay immutable
Compare
📋 localeCompare() vs < / > vs Intl.Collator
localeCompare()
a < b
Intl.Collator
Rules
Language collation
UTF-16 code units
Same as localeCompare (reusable)
Result
Number (<0 / 0 / >0)
Boolean
compare(a, b) number
locales / options
Yes
No
Yes (set once)
Best for
One-off compares / small sorts
Quick code-unit checks
Sorting large lists
Hands-On
Examples Gallery
Examples follow MDN String.localeCompare() patterns. Use View Output or Try It Yourself for each case. Outputs use Math.sign where engines may differ on exact integers.
📚 Getting Started
Read before / after / equal from the return value.
Example 1 — Basic localeCompare()
MDN-style: negative, positive, and zero.
JavaScript
Math.sign("a".localeCompare("c")); // -1 (before)
Math.sign("check".localeCompare("against")); // 1 (after)
"a".localeCompare("a"); // 0 (equal)
// In sort callbacks, return the number directly:
["c", "a", "b"].sort((x, y) => x.localeCompare(y));
// ["a", "b", "c"]
The comparator returns the localeCompare number so sort can place each pair. For large lists, create one Intl.Collator("fr", { ignorePunctuation: true }) and reuse .compare.
Without numeric, strings compare digit-by-digit, so "10" can come before "2". With numeric: true, embedded numbers compare as numbers.
Applications
🚀 Common Use Cases
Sorting UI lists — names, titles, countries in the user’s language.
Case- / accent-insensitive equality — sensitivity: "base" and check for 0.
Natural number order in strings — numeric: true for "item2" vs "item10".
Locale-specific rules — pass "de", "sv", "fr", etc.
Large sorts — prefer a reused Intl.Collator.
Not for simple contains checks — use includes() / indexOf() instead.
🧠 How localeCompare() Decides Order
1
Receive the other string
Coerce compareString; read optional locales and options.
Input
2
Apply collation rules
Use language-aware ordering (via Intl.Collator when available).
Collate
3
Honor sensitivity / numeric / …
Options can ignore accents, treat digits as numbers, and more.
Options
4
✅
Return a sort number
Negative, zero, or positive — perfect for Array.sort.
Important
📝 Notes
Do not rely on exact -1 / 1 — only the sign (or 0) is guaranteed.
Pass locales when the UI language must control sort order.
Omitting the compare string (or passing undefined) compares against "undefined".
For many comparisons with the same rules, reuse Intl.Collator.
< / > are not locale-aware — prefer localeCompare for human-facing order.
Results can still differ slightly across engines for edge cases; lock locales + options for consistency.
Compatibility
Browser & Runtime Support
String.prototype.localeCompare() is Baseline Widely available. The locales and options arguments are supported in modern engines via Intl.
✓ Baseline · Widely available
String.localeCompare()
Safe for production sorting and comparisons. Prefer Intl.Collator when sorting large arrays with the same options.
UniversalWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
localeCompare()Excellent
Bottom line: Use String.localeCompare() for locale-aware order. Check the sign of the result (not exact -1/1), pass locales when language matters, and reuse Intl.Collator for big sorts.
Wrap Up
Conclusion
String.prototype.localeCompare() compares strings with language-aware rules and returns a number for sorting: negative, zero, or positive. Use locales and options for accents, case, and numeric order — and reach for Intl.Collator when you sort large lists often.
Pass the UI language in locales when order matters
Use { numeric: true } for natural number order in strings
Reuse Intl.Collator for large sorts
Return localeCompare directly from sort comparators
❌ Don’t
Assert exact -1 or 1 in tests
Assume < / > match dictionary order
Forget accents/case when you meant sensitivity: "base"
Call localeCompare with heavy options millions of times without a collator
Omit the compare string and expect a useful default
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.localeCompare()
Locale-aware sort numbers — negative, zero, or positive.
5
Core concepts
📝01
Returns
<0 / 0 / >0
API
🌐02
Locales
language tags
i18n
⚡03
Options
sensitivity / numeric
Tune
📈04
Sort
Array.sort helper
Pattern
📄05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.localeCompare(compareString, locales?, options?) returns a number that tells you the sort order: negative if this string comes before compareString, positive if after, and 0 if they are equivalent for that locale/options.
No. The specification only requires a negative or positive number (or 0). Some engines return -1/1; others may return -2/2. Use < 0, > 0, or Math.sign().
When sort order must match a language (for example German vs Swedish) or when you need sensitivity, numeric sorting, or ignorePunctuation. Without them, the host locale is usually used.
Operators like < compare UTF-16 code units. localeCompare uses language-aware collation rules (and can ignore case/accents with options).
For sorting many strings, yes. Create one Intl.Collator and reuse its compare method — faster than calling localeCompare on every pair with the same locales/options.
No. Strings are immutable. localeCompare only reads and returns a number.
Did you know?
You can enable numeric collation with a Unicode locale extension instead of an options object: "en-u-kn-true". That is the same idea as { numeric: true }, packed into the language tag.