CKTIS / JavaScript
DOMRect.width — rectangle width
DOMRect.width is a writable instance property describing a rectangle's width. It holds a floating-point number and permits negative values. Do not confuse it with the read-only DOMRectReadOnly.width property.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
rect.width
rect.width = valueDOMRect.width describes the width of the rectangle represented by a DOMRect object. It is an instance property: read it as rect.width and change it with rect.width = value. Its Web IDL type is unrestricted double, represented in JavaScript as number. The value need not be an integer or positive; the type also permits NaN, Infinity, and -Infinity.
The constructor new DOMRect(x, y, width, height) accepts the width as its third argument. A negative width is not automatically converted to a positive value. When x and width are finite and width is negative, left is x + width and right is x. This shortcut does not always hold for infinite inputs: with x = Infinity and width = -Infinity, x + width is NaN, so the maximum calculation for right returns NaN, not x.
In exact arithmetic with finite x and width and a negative width, right - left would equal the absolute value of width, but floating-point rounding in JavaScript can change the result. For example, with x = 1e20 and width = -8, x + width rounds to x, so right - left is 0, not 8.
DOMRect inherits from DOMRectReadOnly, but makes width writable. What the rectangle represents depends on the API that returned it; the property alone does not specify the kind of area being measured.
This is a Web API feature described in Geometry Interfaces Module Level 1, not a feature of a particular ECMAScript edition. MDN marks it as widely available across browsers since January 2020, including in Web Workers. The example uses explicit coordinates, so it does not depend on page layout.
Examples
const rect = new DOMRect(10, 20, 30.5, 40);
console.log(rect.width);
rect.width = -8;
console.log(rect.width);
console.log(rect.left, rect.right);Result
30.5 -8 2 10
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.