Use JSON.stringify():

var x = document.getElementById("result");
x.textContent = JSON.stringify( myArray );
Answer from Sirko on Stack Overflow
🌐
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 - ... Well to convert it into a string while preserving its brackets and quotes we can just use the JSON.stringify() method. The JSON.stringify() method converts a JavaScript object or value to a JSON (JavaScript Object Notation) string
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › array-tostring
Array toString() in JavaScript
The `trim` option in Mongoose schema definitions makes Mongoose automatically call `String.prototype.trim()` on string fields. Here's how it works. ... Here's how JavaScript arrays' `flatMap()` method works, and what you can use it for..
🌐
Jsremote
jsremote.jobs › tutorials › array_to_string
Array method toString() in JavaScript | Web developer jobs
For example, when you work with a server that does not accept arrays as arguments. JavaScript offers an option for such cases - the toString() method. This function does not require any parameters.
🌐
Favtutor
favtutor.com › articles › convert-array-to-string-javascript
Convert Array to String JavaScript (5 Easy Methods)
November 25, 2023 - It automatically converts the data types from one to another. In the case of converting an array to a string, JavaScript implicitly coerces the array to a string using the concatenation operator (+).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › toString
Array.prototype.toString() - JavaScript | MDN
The toString method of arrays calls join() internally, which joins the array and returns one string containing each array element separated by commas. If the join method is unavailable or is not a function, Object.prototype.toString is used instead, returning [object Array]. ... const arr = ...
🌐
ReqBin
reqbin.com › code › javascript › ot8wzla9 › javascript-array-to-string-example
How do I convert array to string in JavaScript?
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.
🌐
sebhastian
sebhastian.com › javascript-array-string
Convert a JavaScript array to string | sebhastian
February 1, 2023 - As you can see, you can specify an empty string to get a string without commas. You can convert a nested array into a string in JavaScript using the toString() method.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › join
Array.prototype.join() - JavaScript | MDN
The join method is accessed internally by Array.prototype.toString() with no arguments. Overriding join of an array instance will override its toString behavior as well. Array.prototype.join recursively converts each element, including other arrays, to strings.
🌐
Codedamn
codedamn.com › news › javascript
How to convert a JavaScript array to a string (with examples)?
October 27, 2022 - JavaScript is a scripting language that was initially built to add interactivity to web pages but now it has become the heart of web development. Arrays and strings are one of the most widely used concepts in JavaScript today. This article explains how to convert JavaScript array to stings along ...
🌐
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.
🌐
Tabnine
tabnine.com › home › array to string in javascript
Array to String in JavaScript - Tabnine
July 25, 2024 - 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! JavaScript removes them when it stringifies the content.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript: Bracket Notation finding a letter in a string- why brackets when just assigning a variable - JavaScript - The freeCodeCamp Forum
July 26, 2020 - Tell us what’s happening: im looking at the solution here, and my first thought is… why did I have to use a bracket when assigning this variable? var lastLetterOfLastName = lastName[lastName.length - 1]; <<<<this is th…
🌐
ReqBin
reqbin.com › code › javascript › jaarxzpl › javascript-string-to-array-example
How do I convert string to array in JavaScript?
In JavaScript, an array is a special built-in object that allows multiple elements of different types to be stored in a single variable. JavaScript arrays can be created with square brackets "[...]" or using the "new Array()" constructor.
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

🌐
SitePoint
sitepoint.com › javascript
Getting each array inside brackets in associative array - JavaScript - SitePoint Forums | Web Development & Design Community
April 23, 2022 - I need something like for each bracket separated array in myCombinedJSONVar console.log each key value pair. ... I think the real issue is that is it not registering as a valid JSON because there are multiple JSONS inside one object. So, that’s why I am needing a way to split these two JSONS. It’s possible this approach leads nowhere. ... Surrounded with square brackets?
🌐
SitePoint
sitepoint.com › javascript
jQuery - Convert String to Javascript Array - JavaScript - SitePoint Forums | Web Development & Design Community
November 8, 2010 - I have a string like this: var string = “[[1088163336,80],[1088154636,95],[1088150436,75]]” I want to be able to run operations on this string such as string.length, string.each, etc. What do I need to do to convert this string object to an array object. Also note, that it is important to also preserve the string object during the process.
🌐
DEV Community
dev.to › gaelgthomas › array-to-string-without-commas-in-javascript-4mg6
Array to String Without Commas in JavaScript - DEV Community
September 18, 2022 - In this article, you’ll discover how to convert an array to a string without commas. By no commas, I mean no separator between your array elements (words) or a separator different than a comma. In JavaScript, all arrays have a built-in method called join().