Emmanuel Akinfulubi

JavaScriptString

Convert a string to uppercase and lowercase in JavaScript

Beginner · 3 min read

Changing text to capitals or to small letters is something you will do all the time: making a heading shout, storing email addresses in one consistent form, or comparing two words without caring how they were typed. JavaScript has two methods for it, toUpperCase and toLowerCase.

They look too simple to need a tutorial, and for English they nearly are. But one German letter, ß, shows that changing case can make a string longer, and that changing it back doesn't always give you the original. In this tutorial you will convert text both ways and see exactly where that happens. You only need to know what a string is.

Upper case and lower case

Capital letters are called upper case, and small letters lower case. Only letters have a case. Digits, spaces and punctuation have no upper or lower form, so they stay exactly as they are.

Strings never change

Strings in JavaScript are immutable: once one is made, it can't be changed. So toUpperCase doesn't change your string. It gives you a new string in capitals and leaves the original alone. If you want to keep the result, store it in a variable.

Here is the exercise. Have a go yourself first, or read on and we will build it together.

13 Upper case, lower case, and the German sharp s

easy

Convert text between cases. Then upper-case 'ß' and check the length of what comes back.

Stuck? Three hints, smallest first
  1. 1. toUpperCase and toLowerCase return new strings; they cannot modify in place.
  2. 2. Digits and punctuation pass through untouched.
  3. 3. Case conversion is not guaranteed to preserve length, and ß is the proof.

Step 1: shout and whisper

Step 1

const shout = (text) => text.toUpperCase();
console.log(shout('lagos'));             // 'LAGOS'
console.log('LAGOS'.toLowerCase());      // 'lagos'
console.log(shout('a-1-b'));             // 'A-1-B'

The dashes and the digit in 'a-1-b' pass through untouched, since they have no case.

Step 2: the original stays the same

Step 2

const city = 'lagos';
const loud = city.toUpperCase();
console.log(loud); // 'LAGOS'
console.log(city); // 'lagos'

A common slip is to write city.toUpperCase(); on its own line and expect city to change. It doesn't. The new string is made and then thrown away, because nothing stored it.

Step 3: accented letters work too

The methods know about far more than the English alphabet.

Step 3

console.log('ÉCOLE'.toLowerCase()); // 'école'
console.log('école'.toUpperCase()); // 'ÉCOLE'

The accent stays on the letter in both directions. This is why you should use these methods rather than doing sums on character codes: the codes trick only works for plain A to Z.

Step 4: the German sharp s

German has a letter ß, called the sharp s. JavaScript's upper-case rules turn it into two letters, SS, the traditional way to write it in capitals.

Step 4

console.log('ß'.toUpperCase());        // 'SS'
console.log('ß'.toUpperCase().length); // 2
console.log('Straße'.toUpperCase());   // 'STRASSE'
console.log('Straße'.length);                 // 6
console.log('Straße'.toUpperCase().length);   // 7

'Straße' (German for "street") has 6 characters. In capitals it has 7. So upper-casing a string can make it longer.

That matters whenever code assumes the length stays the same: a box that holds exactly 10 characters, a position worked out on the original text and then used on the capitalized one, or a column of text padded to line up.

Step 5: try to go back

Now turn the capitals back into small letters.

Step 5

const back = 'Straße'.toUpperCase().toLowerCase();
console.log(back);                             // 'strasse'
console.log(back === 'Straße'.toLowerCase());  // false

You get 'strasse', with two s letters, not 'straße'. Once ß became SS, there is no way to know it was ever a single letter. Changing case can't always be undone.

Step 6: what this means for comparing words

You will often compare two words "ignoring case" by changing both to the same case. The ß result shows that the direction you choose matters:

Step 6

console.log('STRASSE'.toUpperCase() === 'straße'.toUpperCase()); // true
console.log('STRASSE'.toLowerCase() === 'straße'.toLowerCase()); // false

Upper-casing makes both sides 'STRASSE', so they are equal. Lower-casing gives 'strasse' and 'straße', which aren't. Pick one direction and use it everywhere, or use localeCompare, which the next exercise covers.

The full solution

Here is the complete answer: the simple conversions, the sharp s growing to two letters, and the round trip that doesn't come back. Press Run to see the output.

The answer

const shout = (text) => text.toUpperCase();

console.log('ß upper-cased :', 'ß'.toUpperCase(), ' length:', 'ß'.toUpperCase().length);
console.log('Straße        ->', 'Straße'.toUpperCase(), '->', 'Straße'.toUpperCase().toLowerCase());
console.log('so the round trip does not return the original:', 'Straße'.toUpperCase().toLowerCase() === 'Straße'.toLowerCase());

Output


      
11 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 11 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

11 checks

test('upper-casing is straightforward', shout('lagos'), 'LAGOS');
test('and so is lower-casing', 'LAGOS'.toLowerCase(), 'lagos');
test('digits and punctuation are untouched', shout('a-1-b'), 'A-1-B');
test('the German sharp s becomes TWO letters', 'ß'.toUpperCase(), 'SS');
test('so the string gets longer', 'ß'.toUpperCase().length, 2);
test('a whole word shows it', 'Straße'.toUpperCase(), 'STRASSE');
test('and the round trip does not come back', 'Straße'.toUpperCase().toLowerCase(), 'strasse');
test('which is not the original lower-cased', 'Straße'.toUpperCase().toLowerCase() === 'Straße'.toLowerCase(), false);
test('so never assume case conversion preserves length', 'Straße'.length !== 'Straße'.toUpperCase().length, true);
test('accents survive in both directions', 'ÉCOLE'.toLowerCase(), 'école');
test('and come back unchanged', 'école'.toUpperCase(), 'ÉCOLE');

Case conversion is a Unicode mapping, not a per-character arithmetic trick, and it is not reversible. 'ß' upper-cases to 'SS', so any code that assumes toUpperCase keeps the length — a fixed-width field, an index into the original, a padded column — is wrong on German text.

That also breaks a common shortcut: comparing a.toUpperCase() === b.toUpperCase() reports 'STRASSE' and 'straße' as equal, while comparing the lower-cased forms reports them as different. Pick one direction and be consistent, or use localeCompare, which is the next exercise.

Common mistakes

  • Expecting the original to change. text.toUpperCase() returns a new string. Write text = text.toUpperCase() or store it in a new variable.
  • Assuming the length stays the same. 'ß' becomes 'SS', so the capitalized text can be longer.
  • Assuming you can undo it. 'Straße' upper-cased then lower-cased is 'strasse'. Keep the original if you might need it.

Try it yourself

  1. Write whisper(text) that returns text in lower case, and test it with 'HELLO, World!'.
  2. Write normalizeEmail(email) that trims spaces and lower-cases, so ' Ada@Example.COM ' becomes 'ada@example.com'.
  3. Find the length of 'Fußball' before and after toUpperCase.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.