JavaScript toUpperCase() Method
This tutorial covers the toUpperCase() method, from JavaScript's String object. The toUpperCase() method is used to convert a string to uppercase letters: only uppercase letters will be converted to their lowercase equivalent, numbers and other non-letter characters will remain the same.
toUpperCase() returns a modified copy, leaving the original string untouched.
Related string method:
Opposite to toUpperCase(), the toLowerCase() method converts uppercase strings to lowercase letters.
Using the toUpperCase() Method
The toUpperCase() method is called on a string, and takes no argument. From the earlier charCodeAt() method tutorial, we know that uppercase letters have a different decimal Unicode value than lowercase letters: uppercase "A" (65) is different from lowercase "a" (97).
var carter = "Rhesus Macaque, PhD";
alert( carter.toUpperCase() );
The script above declares a string containing mixed uppercase and lowercase letters in different contexts. Applying the toUpperCase() method on our string yields an all-uppercase returned string.
JavaScript's toUpperCase() method converts all string characters with decimal Unicode value between 97 ("a") and 122 ("z") to their uppercase equivalent (by substracting 32 from their decimal Unicode value, from "A" at 65, to "Z" at 90).
This second example comes closer to a real-life sentence:
var carter = "I learn ECMAScript (ECMA-262), ";
carter += "the standardized core JavaScript language";
alert( carter.toUpperCase() );
The toUpperCase() method converted all lowercase letters to their lowercase equivalent, regardless of their context (start, middle, or end of word):
Test the toUpperCase() Method
Interactively test the toUpperCase() method by editing the JavaScript code below and clicking the Test toUpperCase() Method button.
Browser support for JavaScript toUpperCase() method | |||
---|---|---|---|