Table of contents
1.
Introduction
2.
Syntax
3.
Parameters
4.
Return Value
5.
Examples
5.1.
Example 1: Basic Uppercase Conversion
5.2.
Javascript
5.2.1.
Explanation:
5.3.
Example 2: Using toUpperCase() in a Function
5.4.
Javascript
5.4.1.
Explanation:
5.5.
Example 3: Converting User Input to Uppercase
5.6.
Javascript
5.6.1.
Explanation:
6.
Exceptions of JavaScript String toUpperCase() Method
7.
Supported Browsers
8.
Frequently Asked Questions
8.1.
Does toUpperCase() modify the original string?
8.2.
What happens if the string already contains uppercase letters?
8.3.
Is the toUpperCase() method case-sensitive?
9.
Conclusion
Last Updated: Oct 2, 2024
Medium

JavaScript String toUpperCase() Method

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript String toUpperCase() Method is used to convert a string to uppercase letters. This can be useful in various situations, such as standardizing user inputs or comparing strings without worrying about case sensitivity. 

JavaScript String toUpperCase() Method

In this article, we will learn about how to use the toUpperCase() method in Javascript, along with examples, syntax, and supported browsers.

Syntax

The syntax of JavaScript String toUpperCase() Method is very simple. It does not take any parameters, as it operates directly on the string.

string.toUpperCase()

Parameters

The toUpperCase() method does not accept any parameters. It simply performs task on the string it is called on and converts all its characters to uppercase. This is an example of a method without parameters, making it quite easy to use for beginners.

Return Value

The toUpperCase() method returns a new string with all the characters of the original string converted to uppercase. It does not modify the original string. Instead, it creates a new string, leaving the original one unchanged.

For example:

let lowerCaseString = "coding ninjas";
let upperCaseString = lowerCaseString.toUpperCase();


console.log(upperCaseString);  // Output: CODING NINJAS
console.log(lowerCaseString);  // Output: coding ninjas

 

In this example:

  • upperCaseString stores the result of toUpperCase(), where the string is converted to uppercase.
  • The original lowerCaseString remains unchanged.

Examples

Let's look at some examples to see how the toUpperCase() method works in Javascript.

Example 1: Basic Uppercase Conversion

  • Javascript

Javascript

let greeting = "hello world";

let upperGreeting = greeting.toUpperCase();

console.log(upperGreeting);
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

HELLO WORLD

Explanation:

  • The string "hello world" is converted to "HELLO WORLD".
  • The toUpperCase() method is called directly on the string and returns the new uppercase string.

Example 2: Using toUpperCase() in a Function

You can also use the toUpperCase() method inside a function. This is useful if you need to apply it multiple times in your code.

  • Javascript

Javascript

function convertToUpperCase(text) {

   return text.toUpperCase();

}

let name = "john doe";

console.log(convertToUpperCase(name));
You can also try this code with Online Javascript Compiler
Run Code


Output: 

JOHN DOE

Explanation:

  • The convertToUpperCase function takes a string as input and returns the uppercase version of it.
  • When the function is called with the string "john doe", it outputs "JOHN DOE".

Example 3: Converting User Input to Uppercase

In real-world applications, you may need to convert user input to uppercase to ensure consistency, especially when comparing strings.

  • Javascript

Javascript

let userInput = prompt("Enter your name: ");

let formattedInput = userInput.toUpperCase();

console.log("Welcome, " + formattedInput + "!");
You can also try this code with Online Javascript Compiler
Run Code

 

Output

Enter your name: Gunjan
Welcome, GUNJAN!

Explanation:

  • This code takes user input through prompt(), converts it to uppercase using toUpperCase(), and then prints a welcome message.
  • Regardless of whether the user enters their name in lowercase or mixed case, the output will be in uppercase.

Exceptions of JavaScript String toUpperCase() Method

The toUpperCase() method in JavaScript is straightforward and commonly used string method that doesn't have many exceptions in its basic operation. However, there are a few exceptions that should be taken care of:

  • Locale Insensitivity: By default, toUpperCase() converts letters to uppercase based on the basic or English alphabet. It does not consider locale-specific rules that might apply in languages where there are special rules for converting characters to uppercase. 
     
  • No Effect on Non-Letter Characters: toUpperCase() will not affect any numbers, symbols, or punctuation in a string. It only affects alphabetical characters. Non-alphabet characters remain unchanged.
     
  • Unicode Characters: While toUpperCase() generally handles Unicode characters (such as accented characters) well, there may be some unusual Unicode characters or symbols that it does not affect or that do not have clear uppercase equivalents.
     
  • Immutability of Strings: JavaScript strings are immutable, meaning the toUpperCase() method does not modify the original string; it returns a new string. This is a common feature of JavaScript string methods, not an exception, but it's important to remember in practice.
     
  • Case of Combined Characters: In some languages, characters can be combined to form new characters (e.g., ligatures). The conversion of such combined characters might not always be as expected.

Supported Browsers

JavaScript String toUpperCase() Method is supported across all major browsers, making it a safe and reliable method to use in web development. Below is a list of browsers that support the toUpperCase() method:

  • Google Chrome
     
  • Mozilla Firefox
     
  • Microsoft Edge
     
  • Safari
     
  • Opera
     
  • Internet Explorer (9+)
     

This broad support ensures that your JavaScript code using toUpperCase() will work reliably for users across different browsers and platforms.

Frequently Asked Questions

Does toUpperCase() modify the original string?

No, the toUpperCase() method does not modify the original string. It returns a new string with the characters converted to uppercase, leaving the original string unchanged.

What happens if the string already contains uppercase letters?

If the string already contains uppercase letters, the toUpperCase() method will leave those letters unchanged and only convert the lowercase letters to uppercase.

Is the toUpperCase() method case-sensitive?

The toUpperCase() method is designed to convert only lowercase alphabetic characters to uppercase. It does not affect characters that are already in uppercase or non-alphabetic characters.

Conclusion

In this article, we learned about the JavaScript String toUpperCase() Method, a simple yet powerful tool for converting strings to uppercase. We covered its syntax, parameters, return value, and provided practical examples to help you understand its usage. Additionally, we listed the browsers that supports it. 

You can also check out our other blogs on Code360.

Live masterclass