Web APIs

Element.hasAttribute()

Try the free JavaScript compiler

Check for attribute presence independently of its value, including an empty attribute. Example console output: true false.

Formal Syntax & Structure

javascript syntaxjavascript
element.hasAttribute(name)

Parameters & Attributes

NameTypeDescription
namestringAttribute name.

Return Value

boolean

Whether the attribute is present.

Working Example

Example Snippetjavascript
const p=document.createElement("p"); p.setAttribute("data-ready",""); console.log(p.hasAttribute("data-ready"),p.hasAttribute("data-other"));
JavaScript Reference