author avatar

nived.hari

Wed Feb 19 2025

In JavaScript, you can use localeCompare with { sensitivity: "base" } to compare strings without considering case or accents.
Example:



"Test".localeCompare("test", undefined, { sensitivity: "base" }) === 0; // ✅ True
"café".localeCompare("cafe", undefined, { sensitivity: "base" }) === 0; // ✅ True
"Hello".localeCompare("HELLO", undefined, { sensitivity: "base" }) === 0; // ✅ True


• Case-insensitive
Accent-insensitive
No need for toLowerCase() hacks anymore! 🎉

second argument is locale . by giving it as "Undefined" it uses default locale of the runtime environment. We can specify as "en", "id" etc. It is used in sorting scenarios ig.

Sensitivity is the behaviour of the comparison



"base" → Ignores case & accents ("café" == "cafe", "Hello" == "hello")

"accent" → Considers accents but ignores case ("café" != "cafe", "Hello" == "hello")

"case" → Considers case but ignores accents ("café" == "cafe", "Hello" != "hello")

"variant" → Considers both case & accents ("café" != "cafe", "Hello" != "he



#stimulus #JavaScript #StringComparison