| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
localeCompare() , , (, , , ). Intl.Collator API Intl.Collator.
, , Intl.Collator compare().
const a = "rserv"; // With accents, lowercase
const b = "RESERVE"; // No accents, uppercase
console.log(a.localeCompare(b));
// Expected output: 1
console.log(a.localeCompare(b, "en", { sensitivity: "base" }));
// Expected output: 0
localeCompare(compareString)
localeCompare(compareString, locales)
localeCompare(compareString, locales, options)
locales options locales options , , .
, Intl.Collator API, Intl.Collator(). Intl.Collator , .
compareString, referenceStr. , undefined , localeCompare() "undefined", , .
locales BCP 47 . locales Intl.Collator().
Intl.Collator .
options . options Intl.Collator().
Intl.Collator .
Intl.Collator() locales options.
referenceStr compareString; referenceStr compareString; 0 .
Intl.Collator new Intl.Collator(locales, options).compare(referenceStr, compareString).
, , referenceStr , , compareString.
referenceStr compareString,referenceStr compareString,0 .:
-1 1!
( ), ECMAScript , . -2 2 .
localeCompare()// "" "",
"".localeCompare(""); // -2 -1 ( )
// "" "",
"".localeCompare(""); // 2 1 ( )
// "" "" ,
"".localeCompare(""); // 0
localeCompare() .
const items = ["rserv", "Premier", "Clich", "communiqu", "caf", "Adieu"];
items.sort((a, b) => a.localeCompare(b, "fr", { ignorePunctuation: true }));
// ['Adieu', 'caf', 'Clich', 'communiqu', 'Premier', 'rserv']
locales options locales options .
, "i" (, ) RangeError:
function localeCompareSupportsLocales() {
try {
"foo".localeCompare("bar", "i");
} catch (e) {
return e.name === "RangeError";
}
return false;
}
locales, localeCompare(), . , , , (, , ) locales:
console.log("".localeCompare("z", "de")); // : a
console.log("".localeCompare("z", "sv")); // : z
options, localeCompare(), options:
// a
console.log("".localeCompare("a", "de", { sensitivity: "base" })); // 0
// a
console.log("".localeCompare("a", "sv", { sensitivity: "base" })); //
// , "2" > "10"
console.log("2".localeCompare("10")); // 1
// :
console.log("2".localeCompare("10", undefined, { numeric: true })); // -1
// :
console.log("2".localeCompare("10", "en-u-kn-true")); // -1
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.localecompare |
| ECMAScript 2027 Internationalization API Specification # sup-String.prototype.localeCompare |
This page was last modified on 29 . 2026 . by MDN contributors.
StringString.prototype.anchor()at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()String.prototype.matchAll()String.prototype.normalize()String.prototype.padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()toWellFormed()String.prototype.trim()String.prototype.trimRight()String.prototype.trimLeft()String.prototype.valueOf()String.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |