If we have a complex object, and we’d like to convert it into a string, to send it over a network, or just to output it for logging purposes, we can use
toString
method
let user = {
  name: "John",
  age: 30,
  toString() {
    return `{name1: "${this.name}", age1: ${this.age}}`;
  }
};

document.write(user);
// document.write(JSON.stringify(user));
The code above will produce the following result:
{name1: "John", age1: 30}
by Valeri Tandilashvili
4 years ago
JavaScript
objects
JavaScript tutorial
2
Pro tip: use ```triple backticks around text``` to write in code fences