JavaScript toUpperCase() method
The toUpperCase()
method in JavaScript is used to convert all the characters in a string to uppercase. This method is useful when you want to standardize string data for display or comparison purposes.
Syntax:
Return Value:
- Returns a new string with all the characters converted to uppercase. The original string remains unchanged.
Example 1: Basic Usage
In this example, the method converts the string "Hello, World!"
to "HELLO, WORLD!"
, changing all lowercase letters to their uppercase equivalents.
Example 2: Handling Mixed Case
The toUpperCase()
method works on strings with mixed case.
Here, all letters in the string are converted to uppercase.
Example 3: Original String Remains Unchanged
The original string is not modified by the toUpperCase()
method.
Example 4: Non-Alphabetic Characters
Non-alphabetic characters remain unchanged when using toUpperCase()
.
In this example, the numbers and special characters stay the same, while the letters are converted to uppercase.
Example 5: Locale Considerations
The toUpperCase()
method is case-sensitive and follows the default locale of the environment. For certain characters, the uppercase representation may differ based on the locale.
In this case, the lowercase 'i' is converted to the uppercase 'I', which is the standard representation.
Summary:
- The
toUpperCase()
method converts all characters in a string to uppercase. - It does not change the original string; it returns a new string with the uppercase conversion.
- Non-alphabetic characters are unaffected by the method.
- The method's behavior can depend on the default locale, particularly for special characters.