JavaScript Check for case insensitive values?

JavaScript provides several methods to check for case insensitive values. The most common approaches use toLowerCase(), toUpperCase(), or regular expressions.

Using toLowerCase() Method

Convert both values to lowercase before comparison:

let name1 = "JOHN";
let name2 = "john";

console.log(name1.toLowerCase() === name2.toLowerCase()); // true
console.log("Hello".toLowerCase() === "HELLO".toLowerCase()); // true
true
true

Using Regular Expression

Use the i flag for case insensitive matching:

let allNames = ['john', 'John', 'JOHN'];
let makeRegularExpression = new RegExp(allNames.join("|"), "i");
let hasValue = makeRegularExpression.test("JOHN");
console.log("Is Present=" + hasValue);

// Direct regex approach
let searchText = "Hello World";
let pattern = /hello/i;
console.log(pattern.test(searchText)); // true
Is Present=true
true

Using includes() with toLowerCase()

Check if an array contains a value case insensitively:

let fruits = ['Apple', 'BANANA', 'orange'];
let searchFruit = 'apple';

let found = fruits.some(fruit => 
    fruit.toLowerCase() === searchFruit.toLowerCase()
);

console.log('Found:', found);
console.log('Index:', fruits.findIndex(fruit => 
    fruit.toLowerCase() === searchFruit.toLowerCase()
));
Found: true
Index: 0

Comparison

Method Best For Performance
toLowerCase() Simple string comparison Fast
Regular Expression Pattern matching Slower for simple cases
some() with toLowerCase() Array searching Good for arrays

Conclusion

For simple string comparison, use toLowerCase() or toUpperCase(). Regular expressions are better for complex pattern matching and multiple values.

Updated on: 2026-03-15T23:18:59+05:30

316 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements