I want to Update rel=canonical href using Java Script

Because many people consider this important for SEO, I checked on this again, in light of this confirmation by @KranthiKiranGude

According to developers.google.com, Understand the JavaScript SEO basics:

Googlebot supports web components. When Googlebot renders a page, it flattens the shadow DOM and light DOM content. This means Googlebot can only see content that’s visible in the rendered HTML. To make sure that Googlebot can still see your content after it’s rendered, use the Mobile-Friendly Test or the URL Inspection Tool and look at the rendered HTML.

Because (1) @KranthiKiranGude used his URL Inspection Tool in his testing and (2) he confirmed the canonical was changed as expected in this way, then it follows that per Google, GoogleBot does indeed “see” and registers this DOM content change after the page is rendered.

Reference: