CKTIS / JavaScript
Image — creating an image element
Image creates an HTMLImageElement outside the document tree. Optional arguments set dimensions but neither load an image nor resize its file.
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
new Image(); new Image(width); new Image(width, height)new Image() creates an img element of type HTMLImageElement, much like document.createElement("img"). It does not insert it into the document. Image is an HTML API exposed in the Window environment, not part of ECMAScript; Node 24 in a repository does not establish this API's availability. The constructor is supported by current browser engines.
Optional width and height arguments set the corresponding dimension attributes. They do not change image data; CSS can affect the final displayed size. naturalWidth and naturalHeight describe density-corrected natural dimensions in CSS pixels; they are 0 before the image is available.
Setting src starts asynchronous resource processing, even outside the document tree. Handle load and error events, or fulfillment and rejection of the decode() promise, before displaying the image. complete === true alone does not prove success: it also covers a missing source or an error. Set an appropriate alt when displaying the image.
Examples
const image = new Image(32, 16);
console.log(image instanceof HTMLImageElement);
console.log(image.width, image.height);
console.log(image.isConnected);
console.log(image.naturalWidth, image.naturalHeight);Result
true 32 16 false 0 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.