TypeError: toLowerCase is not a function in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# TypeError: toLowerCase is not a function in JavaScript

The "TypeError: toLowerCase is not a function" error occurs when we call the toLowerCase() method on a value that is not a string.

To solve the error, convert the value to a string or make sure to only call the toLowerCase method on strings.

typeerror tolowercase is not a function

Here is an example of how the error occurs.

index.js
const str = {}; // โ›”๏ธ TypeError: str.toLowerCase is not a function const result = str.toLowerCase();

tolowercase is not a function

We called the String.toLowerCase() method on an object which caused the error.

# Only call the toLowerCase() method on strings

To solve the error, make sure to only call the toLowerCase() method on strings.

You can convert a value to a string by using the String() constructor or the toString() method.

index.js
const num = 246; const result1 = num.toString().toLowerCase(); console.log(result1); // ๐Ÿ‘‰๏ธ 246 const result2 = String(num).toLowerCase(); console.log(result2); // ๐Ÿ‘‰๏ธ 246

only call tolowercase method on strings

The code for this article is available on GitHub

The String() constructor and the toString() method are used to convert the supplied value to a string.

# Check if the value is a string before calling toLowerCase()

Alternatively, you can check if the value is a string before calling the toLowerCase() method.

index.js
const str = null; const result = typeof str === 'string' ? str.toLowerCase() : ''; console.log(result); // ๐Ÿ‘‰๏ธ ""

check if value is string before calling tolowercase

The code for this article is available on GitHub

We used a ternary operator to check if the str variable stores a string.

If it does, the value to the left of the comma is returned, otherwise the value to the right is returned.

You can also use a simple if statement to check if the value is a string.

index.js
const str = 1234567; let result = ''; if (typeof str === 'string') { result = str.toLowerCase(); } console.log(result); // ๐Ÿ‘‰๏ธ ""
If the value is a string, we return the result of calling the toLowerCase method, otherwise, we return an empty string to be consistent.

# Convert all strings in an array to lowercase

If you want to convert all strings in an array to lowercase, use the map() method to iterate over the array and call the toLowerCase() method on each string.

index.js
const arr = ['A', 'B', 'C']; const result = arr.map(str => str.toLowerCase()); // ๐Ÿ‘‡๏ธ ['a', 'b', 'c'] console.log(result);
The code for this article is available on GitHub

The function we passed to the Array.map() method gets called with each element in the array.

On each iteration, we use the str.toLowerCase() method to convert the current string to lowercase and return the result.

The map() method returns a new array containing the values returned from the callback function.

If the error persists, console.log the value you're calling the toLowerCase method on and use the typeof operator to check its type.

index.js
console.log(typeof 'bobby hadz com'); // ๐Ÿ‘‰๏ธ string console.log(typeof []); // ๐Ÿ‘‰๏ธ object console.log(typeof 3.14); // ๐Ÿ‘‰๏ธ number

# Access property on an object or an array element before calling toLowerCase()

If the value is an object, there's a very good chance that you are forgetting to access a specific property on which you need to call the toLowerCase() method.

index.js
// โœ… Access object property before calling toLowerCase const obj = { site: 'BOBBYHADZ.COM', }; const result1 = obj.site.toLowerCase(); console.log(result1); // ๐Ÿ‘‰๏ธ bobbyhadz.com // ------------------------------------------- // โœ… Access an array element before calling toLowerCase const arr = ['BOBBY', 'HADZ', 'COM']; const result2 = arr[0].toLowerCase(); console.log(result2); // ๐Ÿ‘‰๏ธ bobby
The code for this article is available on GitHub

We accessed a property in an object and an element in an array before calling the toLowerCase() method.

# Conclusion

The "TypeError: toLowerCase is not a function" error occurs when we call the toLowerCase() method on a value that is not a string.

To solve the error, convert the value to a string using the toString() method or make sure to only call the toLowerCase method on strings.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev