[Solved] TypeError: trim is not a function

Total
0
Shares

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

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

What is TypeError: trim 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 = ['Hello ', ' World']

// Trim the object
str.trim()

Output

TypeError: str.trim is not a function

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

In the next statement, we call the String.trim() method on the value of the type Array, and hence we get a TypeError: trim 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 = ['Hello ', ' World']

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

Output

The data type is object

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

The String.trim() 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 trim() method on a valid string object

We can easily resolve the issue by using the trim() 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 trim 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.trim())

// Valid string object
const data = "  Hello World  "
console.log(data.trim())

// In case of Array
const arr = ["Microsoft ", " Apple"]
const output = arr.map(str => str.trim());
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 trim() method.

Example – Type check using if/else

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

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

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.trim() : 'The object is not of type string'
console.log(output)

Output

The object is not of type string

Conclusion

The TypeError: trim is not a function occurs if we call a trim() method on the value that is not of a type string. We can resolve the issue by converting the value into a valid string before calling the trim() 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 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
JavaScript String padStart()

JavaScript String padStart()

Table of Contents Hide padStart() ExamplepadStart() SyntaxpadStart() ParameterpadStart() Return ValueExample 1 – How padStart() method works in JavaScript?Example 2 – Working of padStart() method with default value In this tutorial,…
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