In JavaScript, the toLocaleUpperCase() method is used to convert a string to uppercase, taking into account the locale-specific case mappings. This method returns a new string with the transformed uppercase characters.
Usage Examples
Here are a few examples to illustrate the usage of the toLocaleUpperCase() method:
-
Using the default locale:
'Testing'.toLocaleUpperCase() // Output: 'TESTING'
In this example, the toLocaleUpperCase() method is called without specifying a locale. Therefore, it uses the current locale to convert the string to uppercase.
-
Specifying a specific locale:
'Testing'.toLocaleUpperCase('it') // Output: 'TESTING' 'Testing'.toLocaleUpperCase('tr') // Output: 'TESTİNG'
In these examples, the toLocaleUpperCase() method is called with the locales ‘it’ and ’tr’, respectively. The resulting uppercase strings may vary depending on the locale’s case mappings. Hence, ‘TESTING’ remains unchanged for the ‘it’ locale, but becomes ‘TESTİNG’ for the ’tr’ locale.
Comparison with toUpperCase()
It’s worth noting that the toLocaleUpperCase() method differs from the toUpperCase() method as it considers specific locales in transforming the string. While toUpperCase() simply converts the string to uppercase based on standard case mappings, toLocaleUpperCase() takes into account the locale’s case mappings.
By utilizing the toLocaleUpperCase() method, you can accurately convert strings to uppercase, ensuring compatibility with the desired locale-specific rules.
Tags: JavaScript, toLocaleUpperCase(), string manipulation