How to Convert an Array to CSV in JavaScript [3 Ways]

Here are the ways to convert an array to csv in JavaScript:

  1. Using Array.toString()
  2. Using Array.join()
  3. Using Array.split()

Method 1: Using the Array.toString() method

JavaScript Array toString() method returns a string representing the specified array and its elements. For a simple array, it will return comma-separated values.

const arr = [1, 2, 3, 4, 5];

const csv = arr.toString();




Method 2: Using the Array.join() method

Use the combination of the and Array.prototype.join() methods with a comma separator and new line character to convert to a string.

const mainArray = [
  ['name', 'age', 'city'],
  ['Niva', 30, 'New York'],
  ['Khushi', 25, 'San Francisco'],
  ['Anjni', 19, 'Los Angeles']

const mainCSV = => row.join(',')).join('\n');



Niva,30,New York
Khushi,25,San Francisco
Anjni,19,Los Angeles

Method 3: Using the Array.split() method

This is incorrect in the context of converting an array to CSV. The split() method is used to split a string into an array of substrings based on a specified delimiter. It’s the opposite of what you’re trying to achieve.

To convert a 2D array (like a table) to CSV in JavaScript, it’s a bit more involved.

function arrayToCSV(data) {
  return => row.join(",")).join("\n");

const table = [
  ["Name", "Age", "City"],
  ["John", 25, "New York"],
  ["Jane", 30, "Los Angeles"]



Using the Array.split() method

That’s it!