Pegos
Pegos

Reputation: 183

How to get print the console log in variable?

I would like to output:

[2, [23]]

however, when I run:

console.log([2,[23]]);

I get:

(2) [1, Array(1)]

I have tried to use the toString() method, but I got '2,23' instead.

Upvotes: 3

Views: 63

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073968

You can't get the exact same representation (not least because it varies from environment to environment), but JSON.stringify will get close:

var s = JSON.stringify([2,[23]]); // s gets "[2,[23]]"
document.body.appendChild(
  document.createTextNode(s)
);

Upvotes: 5

Related Questions