🔰converting objects
convert an object to other forms.
obj.toString() // object -> string
obj.toLocaleString() // localization
obj.valueOf() // object -> primitive (typically, number)
obj.toJSON() // customization method used by JSON.stringify()replit ⟩ .toJSON()
let point = {
x: 1, y: 2,
toJSON() { return `(${this.x}, ${this.y})` } // custom .toJSON() method
};
JSON.stringify([point]), // '["(1, 2)"]'.toString() ⟩
.toLocaleString() ⟩
.valueOf() ⟩
.toJSON() ⟩
Last updated
Was this helpful?