Problem Statement
How do you compare strings in JavaScript? Explain case-sensitive and case-insensitive comparison.
Explanation
JavaScript provides multiple ways to compare strings.
Equality operators:
Triple equals compares strings by value and is case-sensitive. Returns true only if characters and case match exactly.
Double equals also compares by value but performs type coercion. For strings, it behaves the same as triple equals.
Always use triple equals for string comparison.
Case-sensitive comparison:
Direct comparison with triple equals. Capital and lowercase letters are different. This is the default behavior.
Case-insensitive comparison:
Convert both strings to same case using toLowerCase or toUpperCase before comparing. This is the standard approach.
LocaleCompare for sorting:
The localeCompare method compares strings for sorting purposes. Returns minus 1 if first string comes before second, 0 if equal, 1 if after. Supports locale-aware sorting for international characters. Can be case-insensitive with options.
Common patterns:
Check if strings match. Check if string starts or ends with substring. Check if string contains substring. Sort array of strings.
String comparison is case-sensitive by default, which catches many beginners. Always normalize case when needed for user input.
Code Solution
SolutionRead Only
// CASE-SENSITIVE COMPARISON (default)
const str1 = 'Hello';
const str2 = 'hello';
const str3 = 'Hello';
// Equality
console.log(str1 === str3); // true (exact match)
console.log(str1 === str2); // false (different case)
// Always use === for strings
console.log('hello' === 'hello'); // true
console.log('5' === '5'); // true
// CASE-INSENSITIVE COMPARISON
// Method 1: Convert to lowercase
const name1 = 'JOHN';
const name2 = 'john';
if (name1.toLowerCase() === name2.toLowerCase()) {
console.log('Names match!'); // This runs
}
// Method 2: Convert to uppercase
if (name1.toUpperCase() === name2.toUpperCase()) {
console.log('Names match!');
}
// PRACTICAL EXAMPLES
// User login (case-insensitive username)
function login(username, password) {
const storedUsername = 'JohnDoe';
const storedPassword = 'secret123';
if (username.toLowerCase() === storedUsername.toLowerCase() &&
password === storedPassword) {
return 'Login successful';
}
return 'Invalid credentials';
}
console.log(login('johndoe', 'secret123')); // 'Login successful'
console.log(login('JOHNDOE', 'secret123')); // 'Login successful'
// LOCALE COMPARE (for sorting)
const words = ['banana', 'Apple', 'cherry'];
// Default sort (lexicographic)
words.sort();
console.log(words); // ['Apple', 'banana', 'cherry']
// Case-insensitive sort
words.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
console.log(words); // ['Apple', 'banana', 'cherry']
// localeCompare returns -1, 0, or 1
console.log('a'.localeCompare('b')); // -1 (a comes before b)
console.log('b'.localeCompare('a')); // 1 (b comes after a)
console.log('a'.localeCompare('a')); // 0 (equal)
// Case-insensitive with localeCompare
const result = 'Hello'.localeCompare('hello', undefined, {sensitivity: 'base'});
console.log(result); // 0 (equal, ignoring case)
// SUBSTRING COMPARISON
const text = 'JavaScript Programming';
// startsWith (case-sensitive)
console.log(text.startsWith('Java')); // true
console.log(text.startsWith('java')); // false
// Case-insensitive startsWith
function startsWithIgnoreCase(str, search) {
return str.toLowerCase().startsWith(search.toLowerCase());
}
console.log(startsWithIgnoreCase(text, 'java')); // true
// includes (case-sensitive)
console.log(text.includes('Script')); // true
console.log(text.includes('script')); // false
// Case-insensitive includes
function includesIgnoreCase(str, search) {
return str.toLowerCase().includes(search.toLowerCase());
}
console.log(includesIgnoreCase(text, 'script')); // true