Last updated: Mar 3, 2024
Reading timeยท3 min
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.
Here is an example of how the error occurs.
const str = {}; // โ๏ธ TypeError: str.toLowerCase is not a function const result = str.toLowerCase();
We called the String.toLowerCase() method on an object which caused the error.
toLowerCase()
method on stringsTo 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.
const num = 246; const result1 = num.toString().toLowerCase(); console.log(result1); // ๐๏ธ 246 const result2 = String(num).toLowerCase(); console.log(result2); // ๐๏ธ 246
The String()
constructor and the toString()
method are used to convert the
supplied value to a string.
toLowerCase()
Alternatively, you can check if the value is a string before calling the
toLowerCase()
method.
const str = null; const result = typeof str === 'string' ? str.toLowerCase() : ''; console.log(result); // ๐๏ธ ""
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.
const str = 1234567; let result = ''; if (typeof str === 'string') { result = str.toLowerCase(); } console.log(result); // ๐๏ธ ""
toLowerCase
method, otherwise, we return an empty string to be consistent.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.
const arr = ['A', 'B', 'C']; const result = arr.map(str => str.toLowerCase()); // ๐๏ธ ['a', 'b', 'c'] console.log(result);
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.
console.log(typeof 'bobby hadz com'); // ๐๏ธ string console.log(typeof []); // ๐๏ธ object console.log(typeof 3.14); // ๐๏ธ number
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.
// โ 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
We accessed a property in an object and an element in an array before calling
the toLowerCase()
method.
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.