when I press the toggle button the parameter changes to “toggle=on”. is it possible to make it go to “toggle=off” when I press it again?

i want to make a toggle button for my sidebar in javascript via parameters. when I press the toggle button the parameter changes to “toggle=on”. is it possible to make it go to “toggle=off” when I press it again?

html code:

<div class="toggle" onclick="toggle(this)"> <div class="bar1"></div> <div class="bar2"></div> <div class="bar3"></div> </div>

javascript code:

function toggle(x) {
function addOrUpdateURLParam(key, value) {
    const searchParams = new URLSearchParams(window.location.search)
    searchParams.set(key, value)
    const newRelativePathQuery = window.location.pathname + "?" + searchParams.toString()
    history.pushState(null, "", newRelativePathQuery)
    window.location.replace = newRelativePathQuery
}

addOrUpdateURLParam("toggle", "on");
x.classList.toggle("change");
}