String.prototype.localeCompare(compareString) returns a number that tells you sort order in a given language: negative = before, positive = after, 0 = equivalent. Unlike < / >, it follows locale collation rules.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.localeCompare. Start with this line.
JavaScript
str.localeCompare(other, locales?, options?)
Call
What it does
str.localeCompare(other, locales?, options?)
Primary signature.
string.localeCompare(...)
Call with the arguments your case needs.
Built into JavaScript. Call localeCompare(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
str.localeCompare(other, locales?, options?)
Parameters
Parameter
Description
compareString
Required. Right side to compare against
locales
Optional. Language tag(s), e.g. "de", "fr"
options
Optional. e.g. sensitivity, numeric
Return Value
Return Value
Description
*
Negative / 0 / positive number
JavaScript Examples
Example 1 — Basic localeCompare()
See before / after / equal, then sort a tiny list.
Default string order. Digit-by-digit, "10" can come before "2".
numeric: true. Embedded numbers compare as numbers → 2 before 10.
Sort the list. The same option in sort gives natural order.
Large sorts. Prefer one reused Intl.Collator with { numeric: true }.
Compatibility
Browser & Runtime Support
String.prototype.localeCompare() is widely available. The locales and options arguments follow the Intl Collator API and work in all modern browsers and current Node.js releases.
✓ Baseline · Widely available
String.prototype.localeCompare()
Supported in all evergreen browsers, Internet Explorer 11+ (with Intl), and every current JavaScript runtime with Intl data.
99%Browser support
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
String.localeCompare()Excellent
Bottom line: Safe for modern apps. Pass explicit locales when order must match a language. For heavy sorting, prefer a reused Intl.Collator.
Frequently Asked Questions
localeCompare(compareString, locales?, options?) returns a number for 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.