element.attribute = x
works for W3C defined attributes or already specified node attributes on your element.
This is the exact working as it reads on MDN:
if attributeName is a W3C defined attribute and an attribute node for the element (e.g., id),
that Attribute Node gets assigned the value of x
if attributeName isn't a W3C defined attribute or an attribute node for the element,
the element's (JavaScript object) attributeName property is assigned the value of x
See Syntax section on this page: https://developer.mozilla.org/en-US/docs/Web/API/Node.attributes
Examples
Take below DOM element
<div data-goals="3" id="container"></div>
You would be fine modifying data-goals
through
document.getElementById('container').dataGoals = '4';
You are also able to add new attributes, if these are defined by W3C, e.g. title
document.getElementById('container').title = 'This is a title';
But you should not expect to be able to set a new attribute which is not defined by W3C
document.getElementById('container').myOwnAttribute = 'something';