JavaScript String.toUpperCase() Method
-
Syntax of JavaScript
toUpperCase()
: - Example Codes: Convert All Lowercase Strings to Uppercase
- Example Codes: Convert Toggle Case String to Uppercase
- Example Codes: Convert a String With Digits and Special Characters to Uppercase
The toUpperCase()
method is useful when developers want to toggle the case of the lowercase characters to uppercase. Given that JavaScript strings are immutable, the toUpperCase()
method does not affect the string’s actual value.
The value will be transformed to a string if it is not already one via the toUpperCase()
method, which returns the calling string value in uppercase.
Syntax of JavaScript toUpperCase()
:
string.toUpperCase();
Parameter
This method does not take any parameters.
Return
It converts the string’s case to uppercase.
Example Codes: Convert All Lowercase Strings to Uppercase
The toUpperCase()
method converts the entire string to uppercase. This method does not affect any special characters or numbers already in uppercase.
In this demonstration, all lowercase characters in a string have been changed to uppercase. The method toUpperCase()
turns every character of string input into uppercase characters, which we can see in the below example output.
var referenceString = 'javascript is best for gamers';
var str = referenceString.toUpperCase();
console.log(str);
Output:
JAVASCRIPT IS BEST FOR GAMERS
Example Codes: Convert Toggle Case String to Uppercase
A string with alternate lowercase and uppercase letters is known as a toggle case string. The below example demonstrates how to change a string of characters from lowercase to uppercase.
You can see in the output that the method toUpperCase()
converts all toggle case characters in a string to uppercase.
var str = 'JavAScRipT is A gOOd ProgRamMinG LanGuaGe';
var result = str.toUpperCase();
console.log(result);
Output:
JAVASCRIPT IS A GOOD PROGRAMMING LANGUAGE
Example Codes: Convert a String With Digits and Special Characters to Uppercase
This technique converts all lowercase alphabets to the corresponding uppercase equivalents without affecting the numerals or special characters.
In this illustration, the string st
contains lowercase and uppercase letters, digits, and special characters. All of the lowercase letters have been changed to uppercase.
var st = 'This was a GrE@t 15 mInute Run';
var result = st.toUpperCase();
console.log(result);
Output:
THIS WAS A GRE@T 15 MINUTE RUN
The toUpperCase()
method is supported by most modern browsers. Use the toUpperCase
method to convert a string with uppercase, lowercase, numerals, or special characters to uppercase.