-1

my question is, if I have an array in javascript, let's say :

let x = [1, 2, 3, [4, 5, 6]]

And I want to transform it into a string that would look like this : y = "[1, 2, 3, [4, 5, 6]]", how should I do it? I tried this things:

let a = x.toString();
let b = y.toLocaleString();
let c = new String(x);

But the problem with it is that, all of them look like this:

"1, 2, 3, 4, 5, 6"

So, it completely removes the '[]'. How can I keep the array inside the string like this :

"[1, 2, 3, [4, 5, 6]]"

with the []

1
  • 1
    JSON.stringify(x) would yield "[1,2,3,[4,5,6]]". The other methods also don’t include spaces after commas. Commented Jul 5, 2020 at 15:19

1 Answer 1

2

Does this helps?

 let y = [1, 2, 3, [4, 5, 6]];
 

 var x = JSON.stringify(y);
 
 console.log(typeof(x));
 
 console.log(JSON.stringify(y));

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.