Definition and Usage
The toUpperCase() function returns the given string converted to uppercase.
Features
- The return value is a new string with all characters of the given string converted to uppercase.
- Since it only returns the converted string, the original string remains unchanged.
Basic Example
const originalString = "Hello, World!";
const uppercaseStr = originalString.toUpperCase();
console.log(uppercaseStr); // Output: "HELLO, WORLD!"
/* 👇 The original string remains unchanged. */
console.log(originalString); // Output: "Hello, World!"
To convert all characters of the given string to lowercase, use the toLowerCase() function.
Syntax
str.toUpperCase();
str is the string to which the toUpperCase() function is applied.
This function is intended for strings only. Calling it on a value of any other data type will result in a TypeError.
Parameters
None.
Return value
The return value is a new string with all characters of the given string (str) converted to uppercase. During this process, the original string (str) remains unchanged.
Things to Keep in Mind
The toUpperCase() function is a method that can be used only on strings. If you call this method on a value of any data type other than a string, a TypeError will occur. Such a call is not a valid operation.
const number = 17; // A number, not a string
try {
let result = number.toUpperCase(); // A TypeError occurs here
console.log(result);
} catch (error) {
console.error(error); // TypeError: number.toUpperCase is not a function
}
In the code above, toUpperCase() is used on a number value, so a TypeError is raised. To prevent this, you should check the data type before calling toUpperCase() or convert the value to a string when necessary.
Practical Examples
The toUpperCase() function is one of the most widely used functions in JavaScript. Let's explore this function through various practical examples.
String Comparison
You can compare strings without case sensitivity by converting them to uppercase. The example below uses the toUpperCase() function to convert both strings to uppercase and then checks whether they match.
const str1 = "apple";
const str2 = "APPLE";
if (str1.toUpperCase() === str2.toUpperCase()) {
console.log("The two strings are the same.");
} else {
console.log("The two strings are different.");
}
// Output: "The two strings are the same."
Output Formatting With Uppercase
English text often distinguishes meaning through case usage. Titles, headings, safety notices, and warning messages are frequently written in uppercase, making the toUpperCase() function especially useful for standardizing output.
const title = "web development";
console.log(title.toUpperCase()); // Output: "WEB DEVELOPMENT"
const warningMessage = "keep out of reach of children";
console.log(warningMessage.toUpperCase());
// Output: "KEEP OUT OF REACH OF CHILDREN"
Specifications
| Specification | |
|---|---|
toUpperCase()
|
ECMAScript® 2026 Language Specification #sec-string.prototype.touppercase |
Compatibility
| Method |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
toUpperCase()
|
1 | 12 | 1 | 1 | 0.10 |