String.prototype.toUpperCase()

Returns a new string with every character converted to upper case using locale-independent rules.

Signature
toUpperCase()
  • Added in JavaScript ES1

Return value

stringA new string representing the calling string converted to upper case. The original string is unchanged.

Errors

Throws a TypeError if called on null or undefined via call() or apply().

toUpperCase() maps every character to its upper-case form with the locale-independent Unicode algorithm, so the result never depends on the machine it runs on.

When to reach for it

Use it for values that are conventionally upper case in a protocol or data format: ISO country and currency codes, HTTP methods, hex digests, ticket prefixes, and headings you build for console output. It also pairs naturally with padEnd() when formatting fixed-width labels.

What trips people up

Upper-casing can make a string longer. German ß upper-cases to SS, and the fi ligature becomes FI, so any code that assumes s.toUpperCase().length === s.length — index maths, column alignment, buffer sizing — will drift on real-world text.

It is also not round-trippable. Upper-casing then lower-casing does not always give the original back, which quietly breaks "normalise, store, restore" patterns. And like its lower-case counterpart, it does not remove accents: "É" stays accented.

For text a user reads in their own language, use toLocaleUpperCase(); the default algorithm gets Turkish and a few other scripts wrong on purpose, in exchange for being identical everywhere.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Normalise a two-letter country code before matching it against a table
  • Render section headings in console or report output
  • Format a currency code alongside an amount

Frequently asked questions

How do I capitalize just the first letter of a string?

Combine slice with toUpperCase: str.charAt(0).toUpperCase() + str.slice(1). For text that may start with an emoji or astral character, split with [...str] first so you do not cut a surrogate pair in half.

Why is my uppercase string longer than the original?

Some characters have multi-character upper-case mappings — ß becomes SS and the fi ligature becomes FI. Never assume toUpperCase preserves length or character offsets when the input can contain non-ASCII text.

When should I use toLocaleUpperCase instead?

Use toLocaleUpperCase whenever the result is shown to a user in their own language, because it applies rules such as the Turkish dotted I. Keep toUpperCase for protocol values and keys, where identical output across hosts matters more.

More examples

Special casings that change length

Sharp s expands to SS and the fi ligature to FI, each gaining a code unit. The round-trip line shows the mapping is lossy: upper-casing then lower-casing "ß" yields "ss", not the character you started with.

Example · javascript
const samples = ["ß", "fi", "dž", "a"];

for (const s of samples) {
  const up = s.toUpperCase();
  console.log(`${s} -> ${up}  (${s.length} -> ${up.length})`);
}

console.log("round trip:", "ß".toUpperCase().toLowerCase());

Locale-independent by design

The default algorithm produces the same output on every host, while the Turkish locale variant applies the dotted-I rule. The last line confirms upper-casing never strips an accent — use normalize() plus an accent-removing regex if that is what you need.

Example · javascript
const word = "i̇stanbul";

console.log(word.toUpperCase());
console.log(word.toLocaleUpperCase("tr"));
console.log("É".toUpperCase() === "E");

Discussion

  • Be the first to comment on this lesson.