JavaScript Array into String

In this section, we will learn how we can turn the elements of an array into string.

JavaScript Arrays join() Method

The join() method is used when we want to join the elements of an array and return them as a string value.

Array join() Syntax:


Array join() Method Parameters:

There’s only one optional parameter for the join() method and that is a separator.

By default, if you run this method on an array, it will return the elements of that array all stick to each other (meaning there’s no white space between each element).

But if we want, we can add a separator as the argument to this method. For example, if we set a white space as the argument, then a white space will appear between each element of the target array when the final string value is produced.

Array join() Method Return Value:

The return value of this method is a string value that contains all the elements of the array.

Example: converting array to string in JavaScript

const obj1 = {
   name: "John", 
   age: 1000,
      return ` ${}, ${this.age} `;
const obj2 = {
   name: "Omid",
   age: 29,
      return ` ${}, ${this.age} `;
const obj3 = {
   name : "Jack", 
   age :52,
      return ` ${}, ${this.age} `;
const arr = [obj1,obj2,obj3];
console.log (arr.join(" "));


John, 1000 Omid, 29 Jack, 52

Note: when the elements of an array are object, the execution engine calls the toString() method of each object to get the string values. So you might want to override this method in each object to set the specific values you want.

Example: JavaScript combine arrays

const arr = ["Apple","Google","Microsoft", "Apple","Tesla"];



console.log(arr.join(" "));




Apple Google Microsoft Apple Tesla

Leave a Reply