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!

I hope this post has helped, but before you go I highly recommend that you checkout Pluralsight if you are interested in improving your software engineering skills, such as React, JavaScript, or Typescript (and much more).

Not only does Pluralsight provide you with all the learning material you need but it also let's you learn in a fun, interactive way and gives you the ability to see your current progress and level in a certain skill via their Skill Assessment tool which can help you see what you need to learn next to keep improving!

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

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

Earn more and take your coding career to the next level.

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.