Use JSON.stringify():

var x = document.getElementById("result");
x.textContent = JSON.stringify( myArray );
Answer from Sirko on Stack Overflow
Discussions

Why do I get square brackets when trying to convert array to JSON string using stringify
When I try to convert the array below to a string using JSON.stringify, I get to see empty square brackets only. I tried console.log to debug, but I do see the data I want to convert into a string, so what am I doing wrong here? More on stackoverflow.com
🌐 stackoverflow.com
convert string with square brackets to array or list in Javascript - Stack Overflow
This way everything inside brackets is wrapped into strings and the bracket structure can be parsed into an Array hierarchy using JSON.parse. More on stackoverflow.com
🌐 stackoverflow.com
Why array toString() removes the square bracket? It is especially ugly with nested arrays.
I only ever used toString for debugging output. The contract of "toString" isn't to represent the data structure perfectly but to present something that can be printed to the console. JavaScript doesn't know what data structure you're trying to display, so it does what is can do best. Consider a 3 dimensional array, how can javascript infer how you want to display it: var nByN3dArray = (n)=> Array(n).fill().map( (v,idx)=>Array(n).fill().map( (v,idx)=>Array(n).fill().map( (v,idx)=>idx) ) ); nByN3dArray(4).toString(); produces: '0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3' Basically from what I can tell the language assumes that whatever data structure you're trying to display isn't necessarily inferable so it just flattens the whole things. Could also just be a quirk of the language specification. Who knows. More on reddit.com
🌐 r/javascript
4
0
September 18, 2016
javascript convert string into array of int with brackets - Stack Overflow
I have a string that contains an ... convert that string into array of integers. I can use split function but I also need to strip of square brackets and remove spaces if any between the numbers. ... Try match, then the input doesn't need to be valid JSON.... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
🌐
Programmingbasic
programmingbasic.com › convert-array-to-string-with-brackets-and-quotes-in-javascript
Convert array to string with brackets and quotes in JavaScript
March 27, 2025 - And you want to print it out in ... it into a string while preserving its brackets and quotes we can just use the JSON.stringify() method....
🌐
ReqBin
reqbin.com › code › javascript › ot8wzla9 › javascript-array-to-string-example
How do I convert array to string in JavaScript?
November 21, 2023 - You can also create a multiline string, check if it starts or ends with another string, and get the length of a string. In JavaScript, you can convert an array to a string using the Array.join(separator) and array.toString() methods.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › array-tostring
Array toString() in JavaScript - Mastering JS
December 8, 2021 - Mastering JS · Tutorials Newsletter eBooks Jobs · ☰ · Tutorials Newsletter eBooks Jobs · Tutorials / Fundamentals / Dec 8, 2021 · To convert an array to a string in JavaScript, you can use the toString() method.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-an-array-into-javascript-string
How to convert an array into JavaScript string?
March 15, 2026 - The JSON.stringify() method converts JavaScript objects and arrays into JSON string format. Unlike other methods, it preserves the array structure with square brackets.
🌐
Scaler
scaler.com › home › topics › array to string in javascript
Array to String in JavaScript - Scaler Topics
January 8, 2024 - As you can notice in the output that the square brackets are replaced by quotation marks. This indicates that the given array is converted into a string. ... Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Find elsewhere
🌐
Tabnine
tabnine.com › home › array to string in javascript
Array to String in JavaScript - Tabnine
July 25, 2024 - Let’s look at three examples of converting an array to a string: ... As we can see above, each element is converted to a string, separated with commas, then returned to the user as a single string. Note: the square brackets we used to define the array are gone!
🌐
YouTube
youtube.com › watch
How to convert an array into a string with JavaScript - YouTube
Source code from this tutorial: https://gist.github.com/cferdinandi/f6a9578f0b1524b434d8831bb1822d95Hate the complexity of modern front‑end web development?
Published: April 21, 2023
🌐
Favtutor
favtutor.com › articles › convert-array-to-string-javascript
Convert Array to String JavaScript (5 Easy Methods)
November 25, 2023 - //declaring array let arrNum=[1,2,3,4]; let arrString=['fav','tutor']; //printing and checking the type of arrays console.log(arrNum + " " + typeof(arrNum)); console.log(arrString + " " + typeof(arrString)); //using the json.stringify() function let convertedStrNum = JSON.stringify(arrNum) let convertedStr = JSON.stringify(arrString); //printing and checking type of convertedStr variables console.log(convertedStrNum + " " + typeof(convertedStrNum)); console.log(convertedStr + " " + typeof(convertedStr)); ... We can see the stringified strings with the enclosed array brackets.
🌐
sebhastian
sebhastian.com › javascript-array-string
Convert a JavaScript array to string | sebhastian
February 1, 2023 - let users = [{ name: "John" }, { name: "Lisa" }]; let usersToString = JSON.stringify(users); // ✅ console.log(usersToString); // '[{"name":"John"},{"name":"Lisa"}]' It’s not really a representation of the array values because the return value will still have the square and curly brackets. Still, this is the best way you can convert an array of objects to a string in JavaScript. Sometimes, you want to convert an array into a string without commas.
🌐
YouTube
youtube.com › watch
JavaScript Basics - How To Convert An Array Into A String - YouTube
In this tutorial I'll show you how to convert an array into a string in JavaScript.-------------------------------------------------------------------All my ...
Published: November 18, 2024
Top answer
1 of 4
3

It seems like you can write a pretty straightforward parser:

const parse = (str) => {
  let depth = 0;
  let item = '';
  let items = [];
  for (let i = 0; i < str.length; i++) {
      if (str[i] === '[') {
          depth++;
          if (depth === 2) {
              items.push([]);
          }
      }
      else if (str[i] === ']') {
          if (depth === 3) {
              items[items.length - 1].push(item);
              item = '';
          }
          depth--;
      }
      else if (depth === 3) {
          item += str[i]
      }
  }
  return items;
}
console.log(parse("[[[Class1(a1)],[Class2(a2)],[Price(a1,100)]],[[Class3(a3)],[Price(a3,200)]],[]]"));
console.log(parse("[[],[[Class1(a1)],[Color(a1,200)]],[[IsLight(a1,0)]]]"))

2 of 4
1

function parse(s) {
  return JSON.parse(s
    .replace(/(?<=\[)([^\[\]])/g, "\"$1")
    .replace(/([^\[\]])(?=\])/g, "$1\""));
}

const s1 = "[[[Class1(a1)],[Class2(a2)],[Price(a1,100)]],[[Class3(a3)],[Price(a3,200)]],[]]";
console.log(parse(s1));

const s2 = "[[],[[Class1(a1)],[Color(a1,200)]],[[IsLight(a1,0)]]]";
console.log(parse(s2));

Here is how the regexes work:

  1. A quotation mark is put before every character that is not a bracket, but follows an opening bracket (checked using positive lookbehind).
  2. A quotation mark is put after every character that is not a bracket, but precedes a closing bracket (checked using positive lookahead).

This way everything inside brackets is wrapped into strings and the bracket structure can be parsed into an Array hierarchy using JSON.parse.

IMPORTANT: If you'd also want to run the functions in the strings, and this code runs in the browser, do not use eval! Use a Web Worker instead, which runs in a separate context (here is how).


UPDATE

The code can be simplified to use a single replace:

function parse(s) {
  return JSON.parse(s.replace(/(?<=\[)([^\[\]]+)(?=\])/g, "\"$1\""));
}

const s1 = "[[[Class1(a1)],[Class2(a2)],[Price(a1,100)]],[[Class3(a3)],[Price(a3,200)]],[]]";
console.log(parse(s1));

const s2 = "[[],[[Class1(a1)],[Color(a1,200)]],[[IsLight(a1,0)]]]";
console.log(parse(s2));

Although this version is simpler and faster, it's still much slower than @Dave's parser: https://jsperf.com/https-stackoverflow-com-questions-63048607

🌐
DoFactory
dofactory.com › javascript › arrays
JavaScript Arrays
For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties. This explains why JavaScript arrays are not nearly as fast as arrays in other languages.