• JavaScript Video Tutorials

JavaScript String toUpperCase() Method



The JavaScript String toUpperCase() method converts all the characters in a string to uppercase letters. It does not change the original string, but returns a new string. If the original string is already in uppercase, no changes will be made.

Syntax

Following is the syntax of JavaScript String toUpperCase() method −

toUpperCase()

Parameters

  • It does not accept any parameters.

Return value

This method returns a string with all characters in uppercase.

Example 1

The following example demonstrates the to usage of the JavaScript String toUpperCase() method. It converts the characters of the string "tutorialspoint" into uppercase letters.

<html>
<head>
<title>JavaScript String toUpperCase() Method</title>
</head>
<body>
<script>
   const original_str = "tutorialspoint";
   document.write("Original string is: ", original_str);
   const uppercase_str = original_str.toUpperCase();
   document.write("<br>New string(after converting into uppercase) is: ", uppercase_str);
</script>
</body>
</html>

Output

The above program returns "TUTORIALSPOINT" in the output −

Original string is: tutorialspoint
New string(after converting into uppercase) is: TUTORIALSPOINT

Example 2

Let's take two strings "hello" and "HELLO", and convert one of them, "hello", to uppercase letters. Then, we will compare them and print a statement. The statement will be "Equal" if both strings are the same, or "Not Equal" if they are different.

<html>
<head>
<title>JavaScript String toUpperCase() Method</title>
</head>
<body>
<script>
   const str1 = "hello";
   const str2 = "HELLO";
   document.write("str1 = ", str1);
   document.write("<br>str2 = ", str2);
   document.write("<br>Before conversion:<br>");
   if(str1 === str2){
      document.write("Equal");
   }
   else{
      document.write("Not Equal");
   }
   document.write("<br>After conversion:<br>");
   if(str1.toUpperCase() === str2){
      document.write("Equal");
   }
   else{
      document.write("Not Equal");
   }
</script>
</body>
</html>

Output

After executing the above program, it displays the following statements as −

str1 = hello
str2 = HELLO
Before conversion:
Not Equal
After conversion:
Equal

Example 3

Let's use the charAt() and toUpperCase() methods to convert a specific character in a string to uppercase. In this example, we retrieve a specific character in the string using the charAt() method and try to convert it to uppercase using the toUpperCase() method.

<html>
<head>
<title>JavaScript String toUpperCase() Method</title>
</head>
<body>
<script>
   const str = "Tutorialspoint";
   document.write("String str = ", str);
   document.write("<br>After converting a specific character '", str.charAt(9), "' into in uppercase: ");
   document.write(str.charAt(9).toUpperCase());
</script>
</body>
</html>

Output

The above program converts a specific character 'p' to a uppercase.

String str = Tutorialspoint
After converting a specific character 'p' into in uppercase: P
Advertisements