What You'll Learn
You'll learn how to transform any string into all lowercase or all uppercase letters using JavaScript's built-in case conversion methods.
Why Convert Case?
When working with strings, you often need to change the capitalization. Maybe you're comparing user input (where "HELLO", "hello", and "HeLLo" should all be treated the same), displaying text in a specific style, or normalizing data before saving it. JavaScript gives you two simple methods to handle this: toLowerCase() and toUpperCase().
How It Works
These methods return a new string with all letters converted to the specified case. They don't modify the original string (remember, strings are immutable!).
toLowerCase() converts every letter to lowercase:
let greeting = "HELLO World!";
let quiet = greeting.toLowerCase();
console.log(quiet); // "hello world!"
console.log(greeting); // "HELLO World!" (original unchanged)
toUpperCase() converts every letter to uppercase:
let whisper = "hello world!";
let shout = whisper.toUpperCase();
console.log(shout); // "HELLO WORLD!"
Non-letter characters (numbers, spaces, punctuation) remain unchanged:
let mixed = "JavaScript 2024!";
console.log(mixed.toLowerCase()); // "javascript 2024!"
console.log(mixed.toUpperCase()); // "JAVASCRIPT 2024!"
Real-World Use: Case-Insensitive Comparison
The most common use is normalizing strings for comparison:
let userInput = "YeS";
let answer = "yes";
if (userInput.toLowerCase() === answer) {
console.log("Match!"); // This runs
}
Key Takeaway: Use toLowerCase() and toUpperCase() to transform string case for normalization, comparison, or display—both methods return a new string without changing the original.