javascript
  1. javascript-tolocaleuppercase

JavaScript toLocaleUpperCase()

The toLocaleUpperCase() method is a built-in JavaScript function that converts a string to uppercase while taking into consideration the user's locale. The method returns a new string with all alphabetical characters converted to uppercase, based on the language and regional settings of the user's browser.

Syntax

string.toLocaleUpperCase([locale, options])

The toLocaleUpperCase() method takes two optional parameters:

  • locale (optional) - A string that specifies the language and region to use. Default is the user's browser language and settings.
  • options (optional) - An object that defines additional formatting options, such as for numeric currency or date/time formats.

Example

let name = 'john doe';
let nameUpperCase = name.toLocaleUpperCase();
console.log(nameUpperCase);

Output

JOHN DOE

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JavaScript Output</title>
</head>
<body>

<!-- Your JavaScript code goes here -->
<script>
  let name = 'john doe';

  // Using the toLocaleUpperCase method to convert the string to uppercase
  let nameUpperCase = name.toLocaleUpperCase();

  // Displaying the result in the HTML document
  document.write('<p>' + nameUpperCase + '</p>');
</script>

</body>
</html>

Try Playground

Explanation

In the above example, the toLocaleUpperCase() method is used to convert the string name to uppercase letters, using the default locale settings of the user's browser.

Use

The toLocaleUpperCase() method can be useful for formatting user-entered data or for localizing strings on a website. By taking into account the user's language and region, the method ensures that the string is converted to uppercase in a way that is consistent with the user's expectations.

Important Points

  • The toLocaleUpperCase() method does not modify the original string object; instead, it returns a new string with the uppercase letters.
  • The case conversion performed by toLocaleUpperCase() is based on the Unicode standard, which defines the mapping of upper- to lowercase characters for each language.
  • The toLocaleUpperCase() method is available on all string objects in JavaScript, on both client-side and server-side environments.

Summary

The toLocaleUpperCase() method is a useful function in JavaScript that allows you to convert strings to uppercase while taking into account the user's locale. By using this method, you can ensure that your web applications are able to adapt to the language and regional preferences of your users, making them more accessible and user-friendly.

Published on: