The JavaScript string.toUpperCase() method
The toUpperCase() method returns a new string with every letter converted to uppercase. "hello".toUpperCase() gives "HELLO". With its partner toLowerCase(), it's the standard way to compare strings without caring about case. The original string is never changed.
Overview
toUpperCase() capitalizes every letter in a string and returns the result as a new string. Non-letters are left as they are, and the original is untouched, since strings can't be changed in place.
Beyond obvious display jobs — shouting a heading, formatting a country code — its quiet workhorse role is case-insensitive comparison. To check if two strings match regardless of case, convert both the same way first: a.toLowerCase() === b.toLowerCase(). Most code reaches for toLowerCase() for this, but either works as long as you're consistent.
A small caveat for international text: a few characters don't have a simple one-to-one uppercase form, and locale can matter (the Turkish dotless "i" is the classic example). For those cases there's toLocaleUpperCase(). For everyday English text, toUpperCase() is all you need.
Syntax
const upper = str.toUpperCase()
"hello".toUpperCase() // "HELLO"
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const code = 'us';
const name = 'javascript';
const capitalized = name.charAt(0).toUpperCase() + name.slice(1);
document.getElementById('out').textContent =
code.toUpperCase() + '\n' + capitalized; // US / Javascript
</script>
Best practices
- Use the return value — strings are immutable, so the original is unchanged.
- For case-insensitive comparison, convert both strings the same way first.
- Use
toLocaleUpperCase()when locale-specific rules matter (e.g. Turkish). - To capitalize just the first letter, combine slice() with
toUpperCase()on the first character.
Frequently asked questions
How do I convert a string to uppercase in JavaScript?
str.toUpperCase(). It returns a new all-caps string.Does toUpperCase() change the original string?
How do I compare two strings ignoring case?
a.toLowerCase() === b.toLowerCase().How do I capitalize only the first letter?
s.charAt(0).toUpperCase() + s.slice(1).