Array toString() in JavaScript

Dec 8, 2021

To convert an array to a string in JavaScript, you can use the toString() method. The toString() method returns the elements of the array as a single string without square brackets [ and ].

const array = [1,2,3,4,5];

array.toString(); // "1,2,3,4,5"

The toString() method works well for arrays of primitives, but doesn't work well for arrays of objects. By default, toString() will convert POJO elements into [object Object].

let obj = {a:1, b:2,c:3};
let array = [];

for (let i = 0; i < 3; i++) {
  array.push(obj);
}

array.toString(); // '[object Object],[object Object],[object Object]'

However, toString() is recursive, so instances of classes with custom toString() methods work.

class MyObject {
  toString() {
    return 'test';
  }
}

[new MyObject(), new MyObject()].toString(); // 'test,test'

Printing an Array in Node

In Node.js, you can import the util module and use the inspect() function. This function will print the raw array as a string as shown below:

const array = [1,2,3,4,5];
const {inspect} = require('util');

inspect(array); // [1,2,3,4,5]

When dealing with an array of objects, it prints the result in an easier to read format over toString().

const {inspect} = require('util')

let obj = {a:1, b:2,c:3};
let array = [];

for (let i = 0; i < 3; i++) {
  array.push(obj);
}

inspect(array);
/*
[ { a: 1, b: 2, c: 3 },
  { a: 1, b: 2, c: 3 },
  { a: 1, b: 2, c: 3 } ]
  */

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!