String.prototype.toUpperCase()
toUpperCase() The toUpperCase() method returns a new string with all alphabetic characters converted to uppercase. It does not modify the original string — strings in JavaScript are immutable. Non-alphabetic characters (numbers, punctuation, whitespace) pass through unchanged.
Unicode-aware case folding is built in: accented characters and letters from non-Latin scripts are converted correctly. For scripts where locale affects case conversion (Turkish i → İ, not I), use toLocaleUpperCase() instead.
Syntax
str.toUpperCase()
Parameters
toUpperCase() takes no parameters. It transforms the entire string in one pass, converting every lowercase letter to its uppercase equivalent. Numbers, punctuation, and whitespace are left alone, so calling toUpperCase() on any string is safe regardless of its contents.
Return value
A new string with all lowercase letters converted to uppercase. The original string is not changed.
Examples
Basic usage
const str = "hello world";
console.log(str.toUpperCase()); // "HELLO WORLD"
const mixed = "JaVaScRiPt";
console.log(mixed.toUpperCase()); // "JAVASCRIPT"
Constants and enum values
Uppercasing user input before comparing against a set of known values is a practical pattern for case-insensitive lookups. It avoids the overhead of a regular expression and keeps the comparison readable. This technique is especially useful in value mappers, enum resolvers, and any code path that needs to normalize unpredictable input against a fixed set of uppercase keys:
const status = "active";
const constantStatus = status.toUpperCase();
console.log(constantStatus); // "ACTIVE"
const statuses = {
ACTIVE: "active",
INACTIVE: "inactive",
PENDING: "pending"
};
function getStatusKey(value) {
return Object.keys(statuses).find(
key => statuses[key] === value
)?.toUpperCase();
}
Normalizing user input to uppercase before comparing against constant values is a common pattern for making case-insensitive lookups without reaching for a regex. This approach works well for status codes, role checks, and any fixed-set comparison where the canonical form is uppercase. The same principle applies when you need to derive a short uppercase label from a longer phrase.
Acronym extraction
function toAcronym(text) {
return text
.split(/\s+/)
.map(word => word.charAt(0).toUpperCase())
.join('');
}
console.log(toAcronym("javaScript")); // "JS"
console.log(toAcronym("world wide web")); // "WWW"
Locale-aware conversion
The standard toUpperCase() uses Unicode default case mappings, which work for most languages but miss locale-specific rules. For applications with international users, toLocaleUpperCase() accepts a locale tag and applies the correct mappings for that language:
For specific locales, use toLocaleUpperCase():
const str = "istanbul";
console.log(str.toUpperCase()); // "ISTANBUL"
console.log(str.toLocaleUpperCase('tr')); // "İSTANBUL" (Turkish dotted İ)
console.log(str.toLocaleUpperCase('en')); // "ISTANBUL"
In Turkish, the lowercase i (without a dot) uppercases to İ (with a dot). The standard toUpperCase() does not account for this; toLocaleUpperCase('tr') does. Beyond locale-specific mappings, toUpperCase() handles Unicode characters across all scripts correctly, including languages with accented and non-Latin letters.
Unicode behavior
// Works with accented characters
console.log("çöşğü".toUpperCase()); // "ÇÖŞĞÜ"
// Works with Greek letters
console.log("σ".toUpperCase()); // "Σ"
Common patterns
Combine with trim for input normalization
Chaining trim() and toUpperCase() normalizes user input by removing surrounding whitespace and forcing a consistent case. This pattern shows up in form handlers, search bars, and anywhere raw input needs to be cleaned before processing. The two methods compose cleanly because both return new strings without mutating the original:
function normalizeInput(input) {
return input.trim().toUpperCase();
}
console.log(normalizeInput(" hello ")); // "HELLO"
console.log(normalizeInput(" world ")); // "WORLD"
Create constant-style identifiers
A natural use of toUpperCase() is converting human-readable labels into SCREAMING_SNAKE_CASE constants. Replace spaces with underscores, uppercase everything, and you have a valid JavaScript constant name. This pattern appears in configuration objects, environment variable handling, and code generation tools that need programmatic identifiers from free-text input:
function toConstantName(str) {
return str
.replace(/\s+/g, '_')
.toUpperCase();
}
console.log(toConstantName("max value")); // "MAX_VALUE"
console.log(toConstantName("api key")); // "API_KEY"
Sort strings case-insensitively
JavaScript’s default sort() places uppercase letters before lowercase (Unicode code point order), which produces an unintuitive result for most users. Normalizing to uppercase inside the comparator gives a natural alphabetical order regardless of input casing. Pairing toUpperCase() with localeCompare() ensures the sort respects language-specific rules as well:
const fruits = ["Banana", "apple", "Cherry", "date"];
fruits.sort((a, b) => a.toUpperCase().localeCompare(b.toUpperCase()));
console.log(fruits);
// ["apple", "Banana", "Cherry", "date"]
Key behaviors
- Does not modify the original string — returns a new one
- Numbers and symbols pass through unchanged
- Works for all Unicode characters, including accented letters
- Locale-sensitive versions exist:
toLocaleUpperCase(locale)
toUpperCase() vs toLocaleUpperCase()
For most languages, toUpperCase() and toLocaleUpperCase() produce the same output. The difference appears only in languages where case conversion is context-dependent. Turkish is the most common example: in Turkish, lowercase i (without a dot) maps to uppercase İ (with a dot), not I. Standard toUpperCase() does not know about this rule; toLocaleUpperCase('tr') does.
If your application handles user-generated text in multiple languages, prefer toLocaleUpperCase() and pass the appropriate locale or let it default to the browser’s locale. For ASCII-only text and fixed-locale applications (such as generating CSS class names or URL slugs), toUpperCase() is simpler and faster.
Case-insensitive comparison
A common need is comparing two strings without caring about case. Rather than uppercasing both, use localeCompare with the sensitivity option, which handles Unicode correctly:
const a = "Hello";
const b = "hello";
// Simple approach
console.log(a.toLowerCase() === b.toLowerCase()); // true
// Locale-aware approach (handles Unicode better)
console.log(a.localeCompare(b, undefined, { sensitivity: 'base' }) === 0); // true
See Also
- String.prototype.toLowerCase() — Convert to lowercase
- String.prototype.localeCompare() — Locale-aware string comparison
- String.prototype.trim() — Remove whitespace