hasAttribute() method is used to check and return whether an element has the specified attribute or not.
This method returns Boolean value.
Example - Check if an element has the attribute
- In the next step we are using the if.. else statement.
- In the if statement we are checking if the elem variable has the id attribute.
- Since the id attribute is present, it returns true and the alert statement inside if statement is executed (alert("Given attribute is present");).