Home » Programming » Javascript » Javascript Touppercase

Using the JavaScript toUpperCase() String Method [Examples]

This article shows how the toUpperCase() method is used in JavaScript to make all letters in a string UPPER CASE.

The toUpperCase() method is included in the string object and is fully supported in all browsers.

In the JavaScript programming language, strings can either be a primitive or an object – primitives are the most basic kinds of variables – they have no methods, and simply represent the raw data for the given characters. String objects, however, have additional methods for manipulating and measuring the string – the toUpperCase() is one of these helpful methods.

String variables in JavaScript are initialized as primitives – they will be converted automatically to objects by JavaScript when needed, so you are unlikely to notice the difference.

Syntax

mystring.toUpperCase()

Note that:

  • mystring is the text you wish to change to upper case, and can be any string variable or value
  • The original variable is not modified – the result of calling toUpperCase() will must be assigned to a new variable or used immediately
  • Your text will be returned with all characters converted to upper case
  • You can do the opposite with the toLowerCase() method – converting all characters to lowercase

Example

To use toUpperCase(), call it from your existing string variable, and assign the result to a new variable:

var mystring = "abcdefg!";
var result = mystring.toUpperCase(); 
console.log(result); // "ABCDEFG!"

Conclusion

Converting the case of a string is frequently used when checking whether an email address or username is already in use in an authentication system, or comparing values in arrays. It’s also useful when formatting other user input for storage in a database.

Sometimes, you just want to make some text look REALLY LOUD – toUpperCase() does this effectively.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment