Element

Element.prototype.hasAttribute

Returns a boolean value indicating whether the specified element has the specified attribute or not

Syntax

JavaScript
element.hasAttribute(name)

Parameters

ParameterTypeDescription
namestringThe name of the attribute to check

Return Value

true if the element has the attribute, false otherwise

Examples

Basic Usage
const btn = document.querySelector('button')!
console.log(btn.hasAttribute('disabled'))
Practical Example
const inputs = document.querySelectorAll('input')
const requiredInputs = Array.from(inputs).filter(
  el => el.hasAttribute('required')
)
Advanced Usage
function isExternal(link: HTMLAnchorElement): boolean {
  return link.hasAttribute('target') && link.getAttribute('target') === '_blank'
}

Understanding Element.prototype.hasAttribute

The Element.prototype.hasAttribute method in JavaScript returns a boolean value indicating whether the specified element has the specified attribute or not. It belongs to the Element object and is one of the most widely used methods for working with element values in modern JavaScript and TypeScript applications.

The method signature is element.hasAttribute(name). It accepts 1 parameter: name. When called, it returns true if the element has the attribute, false otherwise. Understanding when and how to use hasAttribute() helps you write more expressive, readable code.

Common use cases for Element.prototype.hasAttribute include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-getattribute, dom-setattribute, dom-removeattribute, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Element.prototype.hasAttribute is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Element Methods

Other methods in the Element object

Related Tools

More Element Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.