HOWTO · JavaScript
Case Insensitive String Comparison in JavaScript
This article explains how to perform comparisons between case insensitive strings in JavaScript. Discover various methods including toLowerCase, toUpperCase, localeCompare, and regular expressions to enhance your string comparison techniques. Perfect for developers looking to improve user experience and ensure reliable string handling in their applications.
On this page
JavaScript string equality is case-sensitive, so "Hello" === "hello" is false. For a straightforward case-insensitive equality check, normalize both strings with toLowerCase() and compare the results. Use localeCompare() when the rules should reflect a language, and a regular expression when the task is matching a pattern rather than comparing two complete strings.
Convert both strings to the same case for simple comparisons
Convert both values the same way before using strict equality. toLowerCase() is a common choice; toUpperCase() can be used by the same pattern, but neither is universally preferable. Case conversion is convenient for ordinary identifiers and user input, though it is not a universal Unicode case-folding operation. If your application must equate particular characters or follow language-specific rules, choose and test the behavior explicitly.
The example compares two values after converting each to lowercase:
const first = "Hello";
const second = "hello";
const equalIgnoringCase = first.toLowerCase() === second.toLowerCase();
console.log(equalIgnoringCase);
true
This works for a simple equality check because both values are normalized before strict comparison. Case conversion creates converted strings; it does not change the original values. For language-sensitive equivalence or ordering, use localeCompare() and choose its options deliberately.
Use localeCompare() when locale-aware comparison matters
localeCompare() compares strings according to collation rules and returns a number: zero means the strings compare as equivalent under the selected options; a negative or positive value indicates ordering. For equality, check whether the result is zero.
The sensitivity option controls which distinctions matter. With "accent", case differences are ignored while accent differences remain significant. With "base", both case and accents may be ignored. The example below compares "café" with "CAFÉ" using "accent", so the values compare as equivalent. A spelling without the accent may also compare as equivalent with "base". Exact collation behavior depends on the locale, so pass a locale when your application requires a particular language’s ordering and verify the strings that matter to your users.
const result = "café".localeCompare("CAFÉ", undefined, { sensitivity: "accent" });
console.log(result === 0);
true
Use this approach when equivalence should follow collation rules. For a plain case-insensitive equality check without locale-specific collation, normalizing both strings is simpler.
Use a regular expression when you need pattern matching
A regular expression is useful for finding text that follows a pattern. Its i flag makes matching case-insensitive, while test() reports whether the pattern matches somewhere in a string. This is different from general string equality: an unanchored expression can match only part of the input. For an exact match against user-provided text, escape regex metacharacters and anchor both ends. In JavaScript, $ can also match before a final line terminator, so the example adds a final-end assertion to require the actual end of the value. It escapes the supplied text before building a RegExp.
const userText = "JavaScript";
const escapedText = userText.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const exactPattern = new RegExp("^" + escapedText + "$(?![\\s\\S])", "i");
console.log(exactPattern.test("javascript"));
true
If the goal is to compare two strings, case conversion or localeCompare() is usually clearer. Avoid adding the g flag to a regular expression reused with test() unless you deliberately manage its lastIndex; global tests can change state between calls. In short, use case conversion for ordinary equality, localeCompare() when language collation affects equivalence, and a regular expression when the input must match a pattern.