[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. Required fields are marked *

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript Anonymous Functions

JavaScript Anonymous Functions

Table of Contents Hide Anonymous Functions Syntax Example 1: Anonymous Functions in JavaScriptExample 2: Anonymous Functions with arguments in JavaScriptExample 3: Using anonymous functions as arguments of other functionsExample 4: Immediate…
View Post
JavaScript String toLowerCase()

JavaScript String toLowerCase()

Table of Contents Hide toLowerCase() ExampletoLowerCase() SyntaxtoLowerCase() ParametertoLowerCase() Return ValueExample: JavaScript Convert string from uppercase to lowercase In this tutorial, we will learn about the JavaScript String toLowerCase() method with…
View Post