JavaScript, a high-level, interpreted scripting language, is a key technology of the World Wide Web. It adds interactivity to websites and powers web applications. A core feature of JavaScript is its ability to manipulate strings. In this article, we will focus on a fundamental aspect of string manipulation – converting a string to uppercase.
There are several reasons you may want to convert a string to uppercase. For example, you might be normalizing data for comparison, or ensuring user input matches a certain format. In JavaScript, this process is straightforward, thanks to the built-in method: toUpperCase().
Method 1: toUpperCase()
The toUpperCase() method is a part of JavaScript’s String object. This method converts all the characters in a given string to uppercase letters.
The syntax is:
var newStr = str.toUpperCase();
Here str is the original string, and newStr is the new string with all characters in uppercase.
Let’s go through a simple example:
let greeting = "Hello World!";
let shout = greeting.toUpperCase();
console.log(shout); // "HELLO WORLD!"
In this example, the toUpperCase() method changes all the lowercase letters in the string ‘Hello World!’ to uppercase, and the console logs ‘HELLO WORLD!’.
Note that the toUpperCase() method does not change the original string. It returns a new string where all the lowercase letters have been converted to uppercase. The original string remains unmodified:
let greeting = "Hello World!";
console.log(greeting); // "Hello World!"
let shout = greeting.toUpperCase();
console.log(shout); // "HELLO WORLD!"
console.log(greeting); // "Hello World!"
In the above example, you can see that the original greeting string remains “Hello World!” even after we’ve created shout with greeting.toUpperCase().
Method 2: toLocaleUpperCase()
While toUpperCase() works in most cases, JavaScript provides another method called toLocaleUpperCase(). This method also converts a string to uppercase, but it is intended to take into account the host environment’s current locale.
For most languages, toLocaleUpperCase() will return the same result as toUpperCase(). However, for languages that have specific rules for uppercase conversion, like Turkish, toLocaleUpperCase() can return different results.
Here’s the syntax:
var newStr = str.toLocaleUpperCase();
Here str is the original string, and newStr is the new string with all characters in uppercase.
Consider this example:
let city = "istanbul";
let cityInUpperCase = city.toLocaleUpperCase('tr-TR');
console.log(cityInUpperCase); // "İSTANBUL"
In this case, toLocaleUpperCase() respects the unique rules of the Turkish language, where ‘i’ in lowercase becomes ‘İ’ in uppercase.
Conclusion
In JavaScript, converting a string to uppercase is a straightforward process thanks to the toUpperCase() and toLocaleUpperCase() methods. While toUpperCase() works for most scenarios, toLocaleUpperCase() is useful when dealing with languages that have specific rules for uppercase conversion. Always remember, these methods do not change the original string but instead return a new string with the changes. Happy coding!