In Javascript, there are some common built-in functions to solve array problems. In this tutorial, I will introduce you to two methods, toString() and join(), to convert an array to a string in JavaScript. Let me clarify them in the next section of the article.
Convert an array to a string in JavaScript
Use array.toString() method
All JavaScript Objects have a toString() method. When we want to use an Object as a string, we can use the toString() method to convert it to a string or when we want to output the Object as a base, we can also use the toString() method to convert it to a string. use toString(). Each converted element is separated by a comma (,).
Syntax:
array.toString()
Example:
var myArray = ['Apple', 'Banana', 'Kiwi'];
var myStr = myArray.toString();
console.log(typeof myArray);
console.log(myStr);
console.log(typeof myStr);
Output:
Object
Apple, Banana, Kiwi
string
In this example, I declare an array with three elements, and each element is a string. I have asked the program to output to the console three lines. One is the type of myArray. The second is the array after converting and, finally, the type of variable used to store the conversion result myStr.
We see that the first line is ‘Object’ which means myArray is an array because arrays are treated as Objects, the following line ‘Apple, Banana, Kiwi’ is a sequence of array elements separated by characters (,), the last line is ‘String’ the variable myStr uses toString() to create is a string that is equal to the result of the second line we have just mentioned above.
Use array.join() method
Array.join() is similar to the toString() method but differs in that it can be passed as a parameter to reformat the separator for each element, while toString() cannot.
Syntax:
array.join(separator)
Parameters
- separator: An optional option to distinguish each array element we convert. Otherwise, the default parameter will be (,).
Now let’s use the join() method without passing arguments to the array like in the example above and follow the results.
Example:
var myArray = "Apple, Banana, Kiwi";
var myStr = myArray.join();
console.log(typeof myArray);
console.log(myStr);
console.log(typeof myStr);
Output:
Object
Apple, Banana, Kiwi
string
Suppose we do not pass parameters – the join() method is no different from the toString() method. The new string also includes the elements of myArray, and each element is separated by a character (,).
Now let’s try to use the join() method and pass as a ‘ – ‘ parameter and see if there is any difference. I will do the following:
var myArray = ['Apple', 'Banana', 'Kiwi '];
var myStr = myArray.join(" - ");
console.log(typeof myArray);
console.log(myStr);
console.log(typeof myStr);
Output:
Object
Apple - Banana - Kiwi
string
We see that between each element of the new array, separated by ‘ – ‘ is no longer separated by commas. So when we convert an array to a string using the join() method, we can change the separator between each element and the toString() method can’t.
Summary
Convert an array to a string in JavaScript is not a difficult thing. This article can be confirmed more. Follow the methods and steps in this article, and you won’t have any trouble with this problem anymore. I hope the article will help your program. Good luck.
Maybe you are interested:
- How to convert Map Keys to an Array in JavaScript
- Remove the last element from an array
- Push multiple Values to an Array in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css