Use Unary Attributes of Assigned Valued HTML Tags

Question:

Is there a difference between using unários attributes alternatively (with a value)? for example:

Standard form:

<script src="/js/" defer></script>

Alternative way 1:

<script src="/js/" defer="defer"></script>

Alternative way 2:

<script src="/js/" defer=true></script>

Alternative way 3:

<script src="/js/" defer="true"></script>

Will it run identically to the default? (other attributes are also unary like disabled ).

Answer:

According to the MDN , "This Boolean attribute is set to indicate to a browser that[…]" or in Portuguese "This Boolean attribute is set to indicate to the browser that[…]". That is, we claim that the attribute is Boolean.

According to this response from user Chuck to a similar question on Stack Overflow , regarding the selected attribute :

"In SGML, an attribute may be minimized so that its value alone is short for both the name and the value, with the only possible value for the attribute in this case obviously being the attribute's own name. HTML uses this for boolean attributes, where the presence or absence of the attribute is what's meaningful, and its value is irrelevant […] You can just write selected."

Basically what he meant by associating with your case:

In SGML ( Standard Generalized Markup Language ), an attribute can be minimized to the value alone when name and value are the same, in this case obviously, you just use the name of the attribute (in your case just use defer ). HTML uses this form for Boolean attributes where the presence of the value or not is irrelevant (in the case of true or defer ). That is, you can just write defer .

Confirming what Chuck describes, according to W3 regarding SGML and HTML: "Boolean attributes may legally take a single value: the name of the attribute itself" or "Boolean attributes can legally take a single value: the name of the attribute itself" .

Still in the same document: "Some attributes play the role of boolean variables[…]. Their appearance in the start tag of an element implies that the value of the attribute is true .

"Some attributes play the role of Boolean variables[…]. Their appearance at the start tag of an element implies that the attribute's value is true .

In other words, when the element is declared it is the same as saying that its value is true :

<script src="/js/" defer></script>

<script src="/js/" defer="true"></script>

<script src="/js/" defer="defer"></script>

<script src="/js/" defer=""></script>

All these statements are valid, however, according to everything that was said above, it is recommended to use the first alternative.


An interesting fact is that when using javascript, but precisely the setAttribute to create these attributes you need to pass two parameters being name and value , in which case you can either pass the value of true , or the empty value:

setAttribute('defer', ''); or setAttribute('defer', 'true');

A tip, for HTML attributes always use the value inside " ", this is the standard form of the language.

Scroll to Top