0

Is there a way to store the result of a substitution parameter/string? For example, I want to store the following output into a variable.

console.log('%j', {foo: 123, bar: 'abc'});

Output:

{"foo":123,"bar":"abc"}

I already know JSON.stringify e.g.

const s = JSON.stringify({foo: 123, bar: 'abc'});
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110

2 Answers2

0

You can use %o and you can see more details, other operators, in console.log following link: https://developer.mozilla.org/en-US/docs/Web/API/console

console.log('test %o test', {foo: 123, bar: 'abc'});
muhammed ikinci
  • 667
  • 2
  • 6
  • 18
0

I have solved it this way:

const x = 'json';
const y = 'string';
const s = `${JSON.stringify({foo: 123, bar: 'abc'})} is a ${x} ${y}`;
console.log(s);
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110