0

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.

3
  • 10
    Did you try JSON.stringify([...])? Commented Sep 7, 2022 at 8:50
  • There are multiple ways to do this, including using join, array + "" etc Commented Sep 7, 2022 at 8:53
  • Also: '["' + input.join('", "') + '"]' Commented Sep 7, 2022 at 8:59

1 Answer 1

0

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.