Quantcast
Channel: Linux Guides, Tips and Tutorials | LinuxScrew
Viewing all articles
Browse latest Browse all 314

Using the JavaScript toUpperCase() String Method [Examples]

$
0
0

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.

View the original article from LinuxScrew here: Using the JavaScript toUpperCase() String Method [Examples]


Viewing all articles
Browse latest Browse all 314

Trending Articles