toUpperCase()
ES3+Returns the string converted to upper case.
Syntax
string.toUpperCase()Return Value
A new string representing the original string in upper case
Examples
const str = 'Hello World';
console.log(str.toUpperCase()); 📌 When to Use
Use toUpperCase() for formatting display text like headings, creating constants-style identifiers, formatting codes (like country/currency codes), or when you need consistent uppercase representation for visual emphasis.
⚠️ Common Mistakes
Using toUpperCase() for case-insensitive comparisons when toLowerCase() is the convention - consistency makes code more readable.
Not considering that some Unicode characters may not have uppercase equivalents or may expand to multiple characters.
Using CSS text-transform: uppercase for display purposes is often better than JavaScript toUpperCase() for visual formatting.
✅ Best Practices
Use CSS text-transform for visual uppercase styling instead of JavaScript when the underlying data should remain mixed case.
For international applications, use toLocaleUpperCase() with the appropriate locale to handle special casing rules correctly.
When creating identifiers or codes, consider if you need uppercase, and document the convention for your team.
⚡ Performance Notes
toUpperCase() has the same performance characteristics as toLowerCase(). It creates a new string object, so cache results when using repeatedly. For strings that are already uppercase, modern engines may optimize to return the same string reference.
🌍 Real World Example
Formatting Codes and Identifiers
toUpperCase() is commonly used to format standard codes like ISO country codes, currency codes, and creating consistent identifiers for storage or display.
// Format ISO country and currency codes
function formatCountryCode(code) {
return code.toUpperCase().trim();
}
console.log(formatCountryCode('us')); // 'US'
console.log(formatCountryCode('Kr')); // 'KR'
// Create constant-style identifier from string
function toConstantCase(str) {
return str
.replace(/([a-z])([A-Z])/g, '$1_$2')
.replace(/[\s-]+/g, '_')
.toUpperCase();
}
console.log(toConstantCase('backgroundColor')); // 'BACKGROUND_COLOR'
console.log(toConstantCase('font-size')); // 'FONT_SIZE'
// Format license plate or product code
function formatProductCode(code) {
return code
.toUpperCase()
.replace(/[^A-Z0-9]/g, '')
.replace(/(.{4})/g, '$1-')
.slice(0, -1); // Remove trailing dash
}
console.log(formatProductCode('abc123def456'));
// Output: 'ABC1-23DE-F456'