I mean sure, if you were writing the website by hand. But every markdown processor I've used is able to derive these from headlines. And everything you can do in JavaScript you can also do when generating the page in the first place if it's just a one time modifications of the page.
Markdown parsers can auto-generate IDs for headers but they don't create a nice icon next when you hover over the header. That's what Anchor.js is for. For an example, see this page: https://kubernetescommunitydays.org/organizing.
Those auto-generated anchors remind me of LaTeX references. You have to write the anchor IDs in manually ... but when you re-word your heading you don't suddenly break all of your links to it.