JSFiddle - React, Tailwind, and code Playground

HTML

<div id="foo">Hi there</div>

JavaScript

function SetTheThing(bMandatory) 
{
    var foo = document.getElementById("foo");

    Log("Value off attribute before setting: " + foo.attributes.contoso);
    Log("HasAttribute before setting: " + foo.hasAttribute("contoso"));

    foo.attributes['contoso'] = "Hello, world!";
    Log("Value off attribute after setting: " + foo.attributes.contoso);
    Log("HasAttribute after setting: " + foo.hasAttribute("contoso"));

    foo.removeAttribute("contoso");
    Log("Value off attribute after removing: " + foo.attributes.contoso);
    Log("HasAttribute after removing: " + foo.hasAttribute("contoso"));

    foo.attributes["contoso"] = null;
    Log("Value off attribute after really removing: " + foo.attributes.contoso);
    Log("HasAttribute after really removing: " + foo.hasAttribute("contoso"));
}

function Log(s) 
{
    var entry = document.createElement("p");
    entry.innerText = s;
    
    document.getElementById("foo").appendChild(entry);
}

SetTheThing(true);