MDN self-hosted library · e01.org

DOMRectReadOnly: toJSON() method

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const rect = new DOMRectReadOnly(10, 20, 100, 50);

const json = rect.toJSON();
console.log(json);
// { x: 10, y: 20, width: 100, height: 50, top: 20, right: 110, bottom: 70, left: 10 }
console.log(typeof json); // "object"
js
const rectJSON = JSON.stringify(rect);
console.log(rectJSON);