[Solved] TypeError: toLowerCase is not a function

Total
0
Shares

If we call the toLowerCase() method on the value that is not of a string type, JavaScript will throw a TypeError: toLowerCase is not a function.

In this tutorial, we will look at what is TypeErrror: toLowerCase is not a function error and how to resolve them with examples.

What is TypeError: toLowerCase is not a function error?

Let us take a simple example to demonstrate this issue

// Declare and store the data into a variable
const str = 123456

// Convert to LowerCase
str.toLowerCase()

Output

TypeError: str.toLowerCase is not a function

In the above example, we have declared a variable and assigned the Number value into it.

In the next statement, we call the String.toLowerCase() method on the value of the type Number, and hence we get a TypeError: toLowerCase is not a function.

We can also check the variable type using typeof() to confirm the datatype.

// Declare and store the data into a variable
const str = 123456

console.log("The data type is", typeof str)

Output

The data type is number

How to fix TypeError: toLowerCase is not a function error?

The String.toLowerCase() method can only be used on the string values and not on any other types. 

There are two ways to fix this issue in JavaScript.

Solution 1: Use the toLowerCase() method on a valid string object

We can easily resolve the issue by using the toLowerCase() method on the valid string values.

If we know the value can be converted to a valid string, then we can use the toString() method in JavaScript that returns the string representing the object. 

If we have a list of string values in an Array we can use Array.prototype.map() which iterates over the each elements of an Array and perform the toLowerCase operation on each string values.

Let us take an example to resolve the issue using the toString() and Array.prototype.map() methods.

// Declare and store the data into a variable
const num =  12345 

// Convert to string object
const str = num.toString()
console.log(str.toLowerCase())

// Valid string object
const data = "HELLo WoRLD"
console.log(data.toLowerCase())

// In case of Array
const arr = ["MICROsOFT", "APPLE"]
const output = arr.map(str => str.toLowerCase());
console.log(output)

Output

12345
hello world
[ 'microsoft', 'apple' ]

Solution 2 – Performing the type check

We can also perform a type check on the variable to check if it’s a string before calling the toLowerCase() method.

Example – Type check using if/else

// Declare and store the data into a variable
const str = 12345

if (typeof num === 'string') {
    console.log(str.toLowerCase())
}

else {
    console.log("The object is not of type string")
}

Output

The object is not of type string

Example – Type check using ternary operator

We can also perform a type check in single line using the Ternary operator as shown below.

// Declare and store the data into a variable
const num = 12345

// Ternary opertor to check if variable is string
const output = typeof num === 'string' ? num.toLowerCase() : 'The object is not of type string'
console.log(output)

Output

The object is not of type string

Conclusion

The TypeError: toLowerCase is not a function occurs if we call a toLowerCase() method on the value that is not of a type string. We can resolve the issue by calling the toLowerCase() method on a valid string or by converting the value into a valid string before calling the toLowerCase() method or by performing a type check; we can mitigate this error.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript Recursive Function

JavaScript Recursive Function

Table of Contents Hide Introduction to the JavaScript recursive functionsJavaScript recursive function examples1) A simple JavaScript recursive function example2) JavaScript Program to Find Factorial of Number Using RecursionWhy don’t we…
View Post
JavaScript String split()

JavaScript String split()

Table of Contents Hide split() Examplesplit() Syntaxsplit() Parametersplit() Return ValueExample 1: JavaScript split() method Example 2: JavaScript Split the string into an array of characters Example 3: JavaScript Splitting a…
View Post
JavaScript String padEnd()

JavaScript String padEnd()

Table of Contents Hide padEnd() ExamplepadEnd() SyntaxpadEnd() ParameterpadEnd() Return ValueExample 1 – How padEnd() method works in JavaScript?Example 2 – Working of padEnd() method with default value In this tutorial,…
View Post
JavaScript Array slice()

JavaScript Array slice()

Table of Contents Hide slice() Exampleslice() Syntaxslice() Parametersslice() Return ValueExample 1: Clone an array using the JavaScript slice() methodExample 2: JavaScript slice() methodExample 3: JavaScript slice() With Negative indexExample 4:…
View Post