javascript
  1. javascript-touppercase

JavaScript toUpperCase()

The toUpperCase() method is a built-in JavaScript function that converts the characters of a string to uppercase letters. This method does not change the original string, but instead, returns a new string with all the characters converted to uppercase.

Syntax

The syntax for the toUpperCase() method is as follows:

string.toUpperCase()

Here, string is the string variable that you want to convert to uppercase.

Example

let name = "john doe";
let upperCaseName = name.toUpperCase();
console.log(upperCaseName);

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";
  let upperCaseName = name.toUpperCase();
  document.write('<p>' + upperCaseName + '</p>');
</script>

</body>
</html>
Try Playground

Explanation

In the above example, the toUpperCase() method is used to convert the name string to uppercase and store the result in the upperCaseName variable. The console.log() statement is used to print the uppercase name to the console.

Use

The toUpperCase() method is useful when you want to convert a string to uppercase for display purposes or for easier comparison of string values. It can be used in various scenarios such as:

  • Formatting user input: You can use the toUpperCase() method to ensure that all input from a user is formatted in uppercase for consistency.
  • Comparing strings: You can use the toUpperCase() method to compare strings without worrying about case sensitivity.
  • Displaying content: You can use the toUpperCase() method to format content for display purposes.

Important Points

Here are some important points to keep in mind while using the toUpperCase() method:

  • The toUpperCase() method is a non-destructive method, which means it does not actually change the original string. Instead, it returns a new string with all the characters in uppercase.
  • The toUpperCase() method only works on the English alphabet. It does not convert non-English letters to uppercase.
  • The toUpperCase() method returns a string, it cannot be used on numbers or other data types.

Summary

The toUpperCase() method is a simple yet powerful JavaScript function that converts the characters in a string to uppercase. It helps in formatting user input, comparing strings, and displaying content among other use cases. Remember to keep in mind the important points while using this method.

Published on: