How do you join an array into a string in JavaScript?

In JavaScript, you can use the join() method to join an array into a string.

This method takes an optional separator as an argument and returns a string that consists of the array elements joined together with the separator.

Let’s explore how to use the join() method with some code examples.

Using the join() method without a separator:

If you call the join() method without passing any arguments, it will join the elements of the array together with a comma (,) separator by default.

For example:

const fruits = ["apple", "banana", "orange"];
const result = fruits.join();
console.log(result); // "apple,banana,orange"

In the above example, we have an array of fruits. We call the join() method on the fruits array and assign the result to the result variable. The join() method joins the elements of the fruits array together with a comma separator, and the resulting string is "apple,banana,orange". We then log the result variable to the console, which outputs the joined string.

Using the join() method with a separator:

You can also pass a separator argument to the join() method to specify a custom separator to use when joining the array elements.

For example:

const fruits = ["apple", "banana", "orange"];
const result = fruits.join("-");
console.log(result); // "apple-banana-orange"

In the above example, we call the join() method on the fruits array with a hyphen (-) separator. The resulting string is "apple-banana-orange", with each element of the fruits array separated by a hyphen. We log the result variable to the console, which outputs the joined string.

Joining an array of numbers:

You can also use the join() method to join an array of numbers. In this case, the resulting string will contain the numbers as strings, not as numeric values.

For example:

const numbers = [1, 2, 3, 4];
const result = numbers.join("-");
console.log(result); // "1-2-3-4"

In the above example, we have an array of numbers. We call the join() method on the numbers array with a hyphen separator. The resulting string is "1-2-3-4", with each number in the numbers array separated by a hyphen. Note that the numbers are not added together or treated as numeric values, but rather as strings.

Joining an array of objects:

If you have an array of objects, you can use the map() method to extract a specific property from each object and then join the resulting array of property values using the join() method.

For example:

const people = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 35 },
];

const names = people.map((person) => person.name);
const result = names.join(", ");

console.log(result); // "Alice, Bob, Charlie"

In the above example, we have an array of person objects, each with a name and age property. We use the map() method to create a new array of just the name properties, which results in the names array containing ['Alice', 'Bob', 'Charlie']. We then call the join() method on the names array with a comma separator, which results in the string "Alice, Bob, Charlie". We log the result variable to the console, which outputs the joined string.

Joining a multidimensional array:

If you have a multidimensional array, you can use the flatMap() method to flatten the array and then join the resulting array of values using the join() method.

For example:

const matrix = [
  [1, 2],
  [3, 4],
  [5, 6],
];

const flat = matrix.flatMap((row) => row);
const result = flat.join(", ");

console.log(result); // "1, 2, 3, 4, 5, 6"

In the above example, we have a multidimensional matrix array. We use the flatMap() method to flatten the matrix array into a one-dimensional array of values, which results in the flat array containing [1, 2, 3, 4, 5, 6]. We then call the join() method on the flat array with a comma separator, which results in the string "1, 2, 3, 4, 5, 6". We log the result variable to the console, which outputs the joined string.

Joining an array with an empty separator:

If you pass an empty string ('') as the separator argument to the join() method, it will join the elements of the array together with no separator.

For example:

const fruits = ["apple", "banana", "orange"];
const result = fruits.join("");
console.log(result); // "applebananaorange"

In the above example, we call the join() method on the fruits array with an empty string separator. The resulting string is "applebananaorange", with no separator between the elements of the fruits array. We log the result variable to the console, which outputs the joined string.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: