The JavaScript toLowerCase() method converts all uppercase characters in the given string into lowercase letters. It only converts uppercase letters to lowercase and keeps digits and other special characters unchanged.
Example:
let s = "WOrlD"
console.log(s.toLowerCase())
world
JavaScript toLowerCase() syntax
The syntax of the toLowerCase function is
str.toLowerCase()
Parameters: The toLowercase() method does not accept any parameters. We need a string object to apply this method.
Return Value: The toLowercase() method converts all uppercase letters in the original string to lowercase. It does not modify the original string and returns a new string.
NOTE: If there are any non-alphabetic characters (digits or special characters) within the original string, they remain unchanged.
JavaScript toLowerCase example
The following set of examples helps you understand the toLowerCase Function. In the following JavaScript statement, it converts all characters in the already declared s string into lowercase. For more, please refer to the string functions page.
const s = "TUTORIAL GATEWAY"
const result = s.toLowerCase()
console.log(result)
tutorial gateway
We can apply the toLowerCase() function directly on the string literal, and it works.
console.log("HELLO".toLowerCase())
hello
Example 2: Original string remains unchanged
As we mentioned earlier, the toLowerCase function does not modify the original string. To convert the string to lowercase and display the output, use it inside a console.log() statement. Otherwise, we must assign it to a new string.
const s = "TUTORIAL GATEWAY"
const l = s.toLowerCase()
console.log(s.toLowerCase())
console.log(l)
console.log(s)
tutorial gateway
tutorial gateway
TUTORIAL GATEWAY
Use JavaScript toLowerCase for case-insensitive comparisons
By default, strings are case-sensitive, so to perform a case-insensitive comparison, we must use the toLowerCase(). When we apply the toLowerCase() function on both sides of the string, it converts the string to lowercase, making the string comparison case-insensitive.
function caseCompare(s1, s2) {
return s1.toLowerCase() === s2.toLowerCase();
}
console.log(caseCompare("USER", "User"));
console.log(caseCompare("UseR", "usEr"));
true
true
Convert array elements to lowercase
Apart from applying the toLowerCase() function on individual string objects, we can use it to convert all elements in an array to lowercase.
In the following code, the map() function assigns the toLowerCase() method to every element in an array to convert them to lowercase and creates a new array.
const s = ["APPLE", "BANANA", "GRAPE"]
const l = s.map(fruit =>fruit.toLowerCase());
console.log(l)
[ 'apple', 'banana', 'grape' ]
JavaScript toLowerCase FAQs
Normalize user input
We can use the toLowerCase() method to normalize user inputs to maintain data uniformity. The following program is helpful when the user accidentally types mixed characters with unwanted whitespaces.
function normalize(s) {
return s.trim().toLowerCase();
}
console.log(normalize(' ADmiN '));
admin
Case-insensitive Search
We can use the toLowerCase() function to perform a case-insensitive search on an array of string items to find a product.
const fruits = ['APPLE', 'BANANA', 'GRAPE'];
const s = 'Apple';
let a = fruits.find((x) => x.toLowerCase() === s.toLowerCase());
console.log(a);
APPLE
Capitalize only the first letter
We can use the combination of the toUppercase(), slice(), and the toLowerCase() methods to capitalize the first character (letter) in the original string.
function capitalize(s) {
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
}
console.log(capitalize('new year'));
New year
JavaScript toLowerCase TypeError
When you use the toLowerCase() method on an undefined string object or a null value, it raises a TypeError. Please replace the undefined word with null; it raises the same error.
const s = undefined
console.log(s.toLowerCase())
TypeError: Cannot read properties of undefined (reading 'toLowerCase')