The toUpperCase() Method in JavaScript
In this technical blog post, we will explore the toUpperCase()
method in JavaScript. This method allows you to convert a string to uppercase, returning a new string with all characters in uppercase letters. It is important to note that this method does not modify the original string and does not accept any parameters.
Usage Example
Let’s take a look at an example to better understand the usage of the toUpperCase()
method:
1 | 'Testing'.toUpperCase(); // 'TESTING' |
In the above example, the toUpperCase()
method is called on the string “Testing”. The method converts all characters in the string to uppercase, resulting in the new string “TESTING”.
Handling an Empty String
If you pass an empty string to the toUpperCase()
method, it will return an empty string as well. For example:
1 | ''.toUpperCase(); // '' |
In the above example, the method is called on an empty string, resulting in an empty string as the output.
Comparison with toLocaleUpperCase()
It is worth noting that the toUpperCase()
method is similar to the toLocaleUpperCase()
method in JavaScript. However, there is one key difference - the toUpperCase()
method does not consider any specific locales. It simply converts all characters to uppercase universally, irrespective of the locale settings. On the other hand, the toLocaleUpperCase()
method takes into account the specific locale and language rules when converting to uppercase.
Tags: JavaScript, toUpperCase(), string manipulation