For the Hosts Locale how to convert a string to uppercase letters in JavaScript?


In this tutorial, we will learn how to convert a string to uppercase letters for the host's locale in JavaScript.

While using a locale language to write for a website, we might have to convert the sentence from lowercase to uppercase without changing the structure of the string.

Using the toLocaleUpperCase() Method

In JavaScript, we use the toUpperCase() Method as well as the toLocaleUpperCase() to change the letters of a string to uppercase. Although both methods give similar output the toLocaleUpperCase() is used primarily for local languages.

Syntax

We will use the following syntax to convert the letters of the entire string to uppercase letters.

wordString.toLocaleUpperCase();

Here wordString is the string whose letters are to be converted to uppercase.

Algorithm

Step 1 Create a string and array of similar words in JavaScript.

Step 2 Use .toLocaleUpperCase() to check if both array and string letters can be changed to uppercase.

Step 3 Create the elements in HTML and get the output for the string and array using the document.getElementById() method.

Example

In the below given example, we have created a string and array and used the toLocaleUpperCase() method to check whether the letters can be changed to uppercase.

The letters of the string changed to uppercase. However, this method didn't work for an array, and we can't change the letters of an array to uppercase using the toLocaleUpperCase().

<html> <body> <div id=upperString> </div> <script> let wordString = "JavaScript String"; document.getElementById('upperString').innerHTML = wordString.toLocaleUpperCase(); </script> </body> </html>

Specifying the locale to use the toLocaleUpperCase() in JavaScript

In JavaScript, the toLocaleUpperCase() method will use the default locale to change the letters to uppercase if we don't specify any locale language.

Syntax

We will use the following syntax to specify the locale language and change the letters of the string to uppercase.

let locale = 'es'; 
let spanish = ""; 
upper.innerHTML = spanish.toLocaleUpperCase(locale);

Example

In the below given example, we have created two strings in different languages. We have specified the locale language within the .toLocaleUpperCase() method for the first string.

In the second string, we have created a variable for the locale language and mentioned the variable in the method.

<html> <body> <h2> Specifying the locale to use <i> .toLocaleUpperCase() </i> in JavaScript </h2> <div id=upper> </div> <div id=upperString> </div> <script> let frString = "Ceci est une chaîne."; document.getElementById('upperString').innerHTML = frString.toLocaleUpperCase('fr-FR'); let locale = 'es'; let spanish = "Esta oración está en mayúsculas."; document.getElementById('upper').innerHTML = spanish.toLocaleUpperCase(locale); </script> </body> </html>

Although we can use the method without specifying a locale language, there can be errors if the locale language is less commonly used.

In this tutorial, we discuss the toLocaleUpperCase() method to convert a string to uppercase letters in JavaScript. We saw different example with and without specifying the locale to use this method.

Updated on: 15-Nov-2022

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements