Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLImageElement.alt — image alternative text

HTMLImageElement.alt reads and sets an image's text replacement. The property reflects the alt attribute; an empty string and a missing attribute do not mean the same thing.

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

image.alt
image.alt = "Replacement text";

HTMLImageElement.alt is a writable DOMString property reflecting an img element's alt attribute. Reading returns its value, and assignment updates the attribute. If the attribute is absent, reading returns "".

The text should replace the image's meaning in context, rather than merely describe its appearance. For a decorative image or one duplicating adjacent information, use image.alt = "". A missing attribute is not equivalent to an empty value. alt is not a title tooltip.

The example distinguishes absence from an empty value using hasAttribute("alt") and demonstrates the change visible through getAttribute("alt"). Run it in a browser: this is an HTML DOM interface, not an ECMAScript language feature. Having Node 24 in the repository does not establish availability of this interface.

Examples

const image = document.createElement("img");
console.log(JSON.stringify([image.alt, image.hasAttribute("alt")]));

image.alt = "Orange cat sleeping";
console.log(image.getAttribute("alt"));

image.alt = "";
console.log(JSON.stringify([image.alt, image.hasAttribute("alt")]));

Result

["",false]
Orange cat sleeping
["",true]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources