DOMRectReadOnly: toJSON() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Note: This feature is available in Web Workers.

The DOMRectReadOnly method toJSON() returns a JSON representation of the DOMRectReadOnly object.

Syntax

js
toJSON()

Parameters

None.

Return value

A new object whose properties are set to the values in the DOMRectReadOnly on which the method was called.

Examples

This example creates a DOMRectReadOnly that represents a rectangle at position (10, 20) with a width of 100 and a height of 50. It then calls toJSON() to obtain a JSON representation of the rectangle.

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

const rectJSON = rect.toJSON();
console.log(rectJSON);
// Output: { x: 10, y: 20, width: 100, height: 50, top: 20, right: 110, bottom: 70, left: 10 }

Specifications

Specification
Geometry Interfaces Module Level 1
# dom-domrectreadonly-tojson

Browser compatibility

BCD tables only load in the browser