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

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# TypeError: join is not a function in JavaScript

The "TypeError: join is not a function" occurs when we call the join() method on a value that is not an array.

To solve the error, make sure to only call the join method on valid arrays.

typeerror join is not a function

Here is an example of how the error occurs.

index.js
const arr = {}; // โ›”๏ธ Uncaught TypeError: join is not a function const result = arr.join(',');

join is not a function

We called the Array.join() method on an object and got the error back.

# Only call the Array.join() method on arrays

To solve the error, console.log the value you're calling the join method on and make sure it's a valid array.

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

only call array join method on arrays

The code for this article is available on GitHub

# Check if the value is an Array before calling join()

You can conditionally check if the value is an array by using the Array.isArray method.

index.js
const arr = null; const result = Array.isArray(arr) ? arr.join(',') : ""; console.log(result); // ๐Ÿ‘‰๏ธ ""

check if value is array before calling join

We used the ternary operator, which is very similar to an if/else statement.

If the value is an array, we return the result of calling the join method. Otherwise, we return an empty string.

This way, you won't get an error, even if the value is not an array.

You can also use a simple if statement to check if the value is an array.

index.js
const arr = null; let result = ''; if (Array.isArray(arr)) { result = arr.join(','); } console.log(result); // ๐Ÿ‘‰๏ธ ""
The code for this article is available on GitHub

If the arr variable stores an array, the if block runs where we call the join() method.

# Convert the value to an array before calling join()

If you have an array-like object, use the Array.from() method to convert it to an array before calling join().

index.js
const set = new Set(['bobby', 'hadz', 'com']); const result = Array.from(set).join(','); console.log(result); // ๐Ÿ‘‰๏ธ "bobby,hadz,com"

convert value to array before calling join

We used the Array.from() method to convert a Set object to an array to be able to use the Array.join() method.

You could achieve the same result by using the spread syntax (...).

index.js
const set = new Set(['one', 'two', 'three']); const result = [...set].join(','); console.log(result); // ๐Ÿ‘‰๏ธ "one,two,three"
The code for this article is available on GitHub

# Accessing a property with an array value before calling join()

If you have an object that has an array property, access the property before calling the join() method.

index.js
const obj = { site: ['bobby', 'hadz', 'com'], }; const result = obj.site.join(', '); console.log(result); // ๐Ÿ‘‰๏ธ bobby, hadz, com

We accessed a property on the object that stores an array to be able to call the join() method.

If the value is fetched from a remote server, make sure it is of the type you expect it to be by logging it to the console.

Also, ensure you have parsed it to a native JavaScript array before calling the join method on it.

The Array.join() method concatenates all of the elements in an array using a separator.

The only argument the Array.join() method takes is a separator - the string used to separate the elements of the array.

If a value for the separator argument is omitted, the array elements are joined with a comma ,.

If the separator argument is set to an empty string, the array elements are joined without any characters in between them.

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