Strings
toUpperCase() and toLowerCase()
Make every letter of a string a capital or lowercase.
Why it exists
People type text any way they like: "london", "London", "LONDON" — all the same city. To a computer these are three different strings, so without bringing them to one case a search finds nothing and a username check says "no match". toLowerCase() fixes that in one line: lowercase both sides and compare the meaning, not the spelling.
Where it's used
- Case-insensitive search:
title.toLowerCase().includes(query.toLowerCase()). - Comparing emails and usernames:
Anna@Mail.comandanna@mail.comare one address. - Styling: all-caps buttons, a capital letter in a name.
- Normalizing before saving: tags, slugs, country codes.
Syntax
string.toUpperCase()
string.toLowerCase()Examples
"Hello".toUpperCase(); // → "HELLO"
"LoNdOn".toLowerCase(); // → "london"const query = "lOnDoN";
const city = "London";
city.toLowerCase() === query.toLowerCase(); // → trueconst word = "cat";
word.toUpperCase(); // → "CAT"
word; // → "cat" — the original is unchanged"Order #42: Ready!".toUpperCase(); // → "ORDER #42: READY!"Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
These methods walk the string and replace each letter with its capital or lowercase partner from the Unicode table. Non-letters (digits, spaces, punctuation) stay as they are.
Strings are immutable, so the methods return a new string and the original stays the same. If you don't keep the result, the call changes nothing.
Unicode knows the pairs for most alphabets — Latin, Cyrillic and Greek all work the same. But there are rare quirks: the German ß becomes two letters SS in upper case, so the string's length can change.
Traps
How it differs from…
- Comparison:
===,!==,<,> === compares strings character by character, case included. To make case not matter, bring both strings to one form before comparing.
When to pick something else
If only the first letter should be a capital, convert just that part: text[0].toUpperCase() + text.slice(1). And for styling (all-caps buttons) CSS text-transform: uppercase is often handier — the data stays untouched.