Convert JS array exactly to string

Viewed 50

How can I print a JS array (["a", "b", "c"]) exactly to a string.

When I do ["a", "b", "c"].toString() I will get 'a,b,c'

I want the string: ["a", "b", "c"]

There is probably a neat method but I can't think of one.

1 Answers

If ["a","b","c"] is good enough, you could simply use JSON.stringify():

const arr = ["a", "b", "c"];
const str = JSON.stringify(arr);
console.log(str);

Related