A CSV is a comma-separated values file, which provides data to be saved in a tabular format. In addition, the CSV files are plain-text files, making them easier for the website developer to create. Let’s see how to create csv values from an array.
JavaScript Array to CSV
To convert an array to csv in JavaScript, use either toString() method or join() function. The toString() is a built-in JavaScript method that returns a string with all array values separated by commas. The toString() method does not change the original array.
Syntax
array.toString()
Arguments
The array.toString() method does not take any parameters.
Return value
The array.toString() method returns a String, representing the array’s values, separated by a comma.
Example
Let’s declare an array and convert it into the csv using an array.toString() method.
let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"]
let moneyheist_csv = moneyheist.toString()
console.log(moneyheist_csv)
Output
Tokyo,Nairobi,Berlin,Oslo,Moscow
The toString() method converts an array into a csv string and returns that output. The output string is filled with array values separated by commas.
Using join() method
The array.join() is a built-in JavaScript function that returns an array as a string. The items will be separated by a specified separator. The default separator is comma (,) in the join() method.
The join() function joins the items of an array into a string and returns the string.
Syntax
array.join(separator)
Parameters
The separator is an optional parameter. If omitted, the elements are separated with a comma.
Return value
It returns a string representing the array values, separated by the specified separator.
Example
let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"]
let moneyheist_csv = moneyheist.join()
console.log(moneyheist_csv)
Output
Tokyo,Nairobi,Berlin,Oslo,Moscow
You can see from the output that, by default, the array.join() method returns a string separated by a comma, but you can pass whatever separator you want, and it will return the string separated by the provided value.
That’s it for converting the array to csv value in JavaScript.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. Krunal has experience with various programming languages and technologies, including PHP, Python, and expert in JavaScript. He is comfortable working in front-end and back-end development.