JavaScript String localeCompare() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

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?)
CallWhat 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

ParameterDescription
compareStringRequired. Right side to compare against
localesOptional. Language tag(s), e.g. "de", "fr"
optionsOptional. e.g. sensitivity, numeric

Return Value

Return ValueDescription
*Negative / 0 / positive number

JavaScript Examples

Example 1 — Basic localeCompare()

See before / after / equal, then sort a tiny list.

JavaScript
console.log(Math.sign("a".localeCompare("c")));           // -1
console.log(Math.sign("check".localeCompare("against"))); // 1
console.log("a".localeCompare("a"));                      // 0

console.log(["c", "a", "b"].sort((x, y) => x.localeCompare(y)));
// ["a", "b", "c"]
Try It Yourself

How It Works

  1. Before. "a" sorts before "c" → negative (shown as -1 via Math.sign).
  2. After. "check" sorts after "against" → positive.
  3. Equal. Same string → 0.
  4. In sort. Return the raw localeCompare number; sort only needs the sign.

Example 2 — Different Languages, Different Order

German and Swedish disagree about where "ä" belongs.

JavaScript
console.log(Math.sign("ä".localeCompare("z", "de"))); // -1 (ä before z)
console.log(Math.sign("ä".localeCompare("z", "sv"))); // 1  (ä after z)

console.log("ä".localeCompare("a", "de", { sensitivity: "base" })); // 0
console.log(Math.sign("ä".localeCompare("a", "sv", { sensitivity: "base" }))); // 1
Try It Yourself

How It Works

  1. German. Under "de", "ä" typically sorts near "a" — before "z".
  2. Swedish. Under "sv", "ä" is treated as a later letter — after "z".
  3. Base sensitivity. In German, "ä" and "a" can count as the same base letter → 0.
  4. Pass the UI language. Order should match what users expect in that locale.

Example 3 — Numeric Sorting

Make "2" sort before "10" with numeric: true.

JavaScript
console.log(Math.sign("2".localeCompare("10"))); // 1  (default)
console.log(Math.sign("2".localeCompare("10", undefined, { numeric: true }))); // -1

console.log(
  ["10", "2", "1"].sort((a, b) =>
    a.localeCompare(b, undefined, { numeric: true })
  )
);
// ["1", "2", "10"]
Try It Yourself

How It Works

  1. Default string order. Digit-by-digit, "10" can come before "2".
  2. numeric: true. Embedded numbers compare as numbers → 2 before 10.
  3. Sort the list. The same option in sort gives natural order.
  4. Large sorts. Prefer one reused Intl.Collator with { numeric: true }.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

Next: toString()

Learn how string objects convert to primitive string values.

toString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful