TypeError: trim is not a function in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
2 min

banner

# TypeError: trim is not a function in JavaScript

The "TypeError: trim is not a function" error occurs when we call the trim() 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 trim method on strings.

typeerror trim is not a function

Here is an example of how the error occurs.

index.js
const arr = [' hey ', ' there ']; // โ›”๏ธ TypeError: trim is not a function arr.trim();

trim is not a function

We called the String.trim() method on an array, which caused the error.

# Make sure to only call the trim() method on strings

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

index.js
const str = ' hello '; const result = str.trim(); console.log(result); // ๐Ÿ‘‰๏ธ "hello"

only call trim on strings

The code for this article is available on GitHub

The trim() method removes the leading and trailing whitespace from the string.

# Check if the value is of type string before calling trim()

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

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

check if value is of type string before calling trim

We used the 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 achieve the same result.

index.js
const str = null; let result = ''; if (typeof str === 'string') { result = str.trim(); } console.log(result); // ๐Ÿ‘‰๏ธ ""
The code for this article is available on GitHub
If the value is a string, we return the result of calling the trim method, otherwise, we return an empty string for consistency.

# Call the trim() method on all strings in an Array

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

index.js
const arr = [' bobby ', ' hadz ', ' com ']; const result = arr.map(str => str.trim()); // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 'com' ] console.log(result);

calling trim method on all strings in array

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 call the String.trim() method on the current element 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 trim method on and check its type using the typeof operator.

index.js
console.log(typeof 'bobbyhadz.com'); // ๐Ÿ‘‰๏ธ string console.log(typeof null); // ๐Ÿ‘‰๏ธ object console.log(typeof {}); // ๐Ÿ‘‰๏ธ object

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 trim() method.

index.js
// โœ… with Objects const obj = { site: ' bobbyhadz.com ', }; const result1 = obj.site.trim(); console.log(result1); // ๐Ÿ‘‰๏ธ "bobbyhadz.com" // ------------------------------------------- // โœ… with Arrays const arr = [' bobby ', ' hadz ', ' com ']; const result2 = arr[0].trim(); console.log(result2); // ๐Ÿ‘‰๏ธ "bobby"
The code for this article is available on GitHub

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

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