JavaScript toLowerCase() Method
This tutorial introduces the toLowerCase() method, from the JavaScript String object. The toLowerCase() method is used to convert a string to lowercase letters: only uppercase letters will be converted to their lowercase equivalent, numbers and other non-letter characters will remain the same.
The original string is not modified: toLowerCase() returns a lowercase copy.
Related string method:
Opposite to toLowerCase(), the toUpperCase() method converts lowercase strings to uppercase letters.
Using the toLowerCase() Method
The toLowerCase() method is called on a string, and does not take any argument. We know from the charCodeAt() method that uppercase and lowercase letters have a different decimal Unicode value: uppercase "A" corresponds to 65, while lowercase "a" corresponds to 97.
var carter = "ECMA 262";
alert( carter.toLowerCase() );
The script above simply declares a string containing an uppercase acronym, a space, and a three-digit number. Calling the toLowerCase() method on our string only converted our acronym's uppercase letters to lowercase; numbers and whitespace remained untouched.
JavaScript's toLowerCase() method converts all string characters with decimal Unicode value between 65 ("A") and 90 ("Z") to their lowercase equivalent (by adding 32 to their decimal Unicode value, from "a" at 97, to "z" at 122).
Here is a second example, closer to a real-life sentence:
var carter = "I learn ECMAScript (ECMA-262), ";
carter += "the standardized core JavaScript language";
alert( carter.toLowerCase() );
The toLowerCase() method converted all uppercase letters, whether at the start, the end, or the middle of words, to their lowercase equivalent:
Test the toLowerCase() Method
Interactively test the toLowerCase() method by editing the JavaScript code below and clicking the Test toLowerCase() Method button.
Browser support for JavaScript toLowerCase() method | |||
---|---|---|---|