Syntax
str.toUpperCase()
str.toLowerCase()Examples
Basic Case Conversion
Converting a string's case.
const text = "Hello World";
console.log(text.toUpperCase()); // "HELLO WORLD"
console.log(text.toLowerCase()); // "hello world"Case-Insensitive Comparison
A very common real-world use: comparing strings regardless of case.
function isSameWord(a, b) {
return a.toLowerCase() === b.toLowerCase();
}
console.log(isSameWord("Hello", "hello")); // true
console.log(isSameWord("JavaScript", "javascript")); // true
// Also useful for case-insensitive search
const items = ["Apple", "Banana", "Cherry"];
const search = "banana";
const found = items.find(item => item.toLowerCase() === search.toLowerCase());
console.log(found); // "Banana"Best practices
- Convert both sides to the same case before comparing user input against known values, to make comparisons case-insensitive
- Use toLocaleUpperCase()/toLocaleLowerCase() instead when working with locale-specific characters, like Turkish "İ"/"i"
- Remember these return new strings - the original is never modified
- Store data in a consistent case (like all lowercase) if you frequently need case-insensitive lookups, rather than converting on every comparison
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification