Blog / Javascript

How to convert an Array into a String in JavaScript

How to convert an Array into a String in JavaScript

In this post find out about how to convert an Array into a String in JavaScript with and without commas as well as converting an array of objects to a string

Will MaygerWill Mayger
August 22, 2021
Article

You might find yourself needing to convert an array into a string in JavaScript, for a number of reasons.

In this post we will cover how to convert an array into a string with commas, without commas, and how to convert an array of objects into a string in JavaScript as well.

It is actually really simple and easy to be able to do, so let’s get stuck in.

Which method converts an array to string in JavaScript

There are two methods we can use to convert an array to string in JavaScript, either the Array.prototype.toString method, or the Array.prototype.join method.

Using toString keeps things nice and simple, but using the join method gives us much more control.

Let’s take a look at how we can convert an Array into a String in JavaScript.

How to convert an Array into a String in JavaScript

Firstly we are going to look at how we can convert an array to a string in javascript with commas using Array.prototype.toString and Array.prototype.join.

[1, 2, 3, 4, 5].toString() // "1,2,3,4,5"
[1, 2, 3, 4, 5].join() // "1,2,3,4,5"
convert array to string javascript with commas

As you can see both of these methods convert the array into a string with commas.

How to convert an Array into a String in JavaScript without commas

To convert an array to a string in JavaScript without commas, we need to use the Array.prototype.join method because it can allow us to provide an argument to say how we want our string to be.

If you want to convert the array into a string without commas, and instead with spaces then all you need to do is pass a space into the Array.prototype.join method.

Here is an example of how to convert an Array into a String in JavaScript without commas using the join method:

[1, 2, 3, 4, 5].join(" ") // "1 2 3 4 5"
[1, 2, 3, 4, 5].join("-") // "1-2-3-4-5"
javascript array to string without commas

How to convert an array of objects to a string in JavaScript

When converting an array of objects into a string in JavaScript, it is a little different in comparison with simple arrays, but equally as easy.

Instead of using Array.prototype.toString, or Array.prototype.join, we instead need to use a function called JSON.stringify.

This function will convert any JavaScript object or array into a string version of it that can be saved as JSON data.

Here is how to use JSON.stringify to convert an array of objects to a string in JavaScript:

JSON.stringify([{ apples: 10 }, { apples: 15 }]) // '[{"apples":10}, {"apples":15}]'
convert array of objects to string javascript

Summary

There we have how to convert an Array into a String in JavaScript, if you want more like this be sure to check out some of my other posts!

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app

Some graphics used on this post were made using icons from flaticon.

Latest Posts

Learn React, JavaScript and TypeScript

Learn React, JavaScript and TypeScript

Join the platform that top tier companies are using.
Master the skills you need to succeed as a software engineer and take your career to the next level with Pluralsight.

Start here

Become an expert in ReactJS, TypeScript, and JavaScript.

Here you will find my personal recomendations to you, for full disclosure I earn a small commission from some of these links, but I only recommend what I trust and personally use.

Good things are coming, don't miss out!

Good things are coming, don't miss out!

Follow me on Twitter to stay up to date and learn frontend, React, JavaScript, and TypeScript tips and tricks!

Are you a novice, intermediate or expert react engineer?

Find out here by taking my fun, interactive, quick quiz which takes approximately 1 - 3 minutes. How well will you will do?

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app