Google's John Mueller said in a Reddit thread that Google will typically use the most restrictive crawling command or signals they find on the page. "Google will use the most restrictive setting you have on the page," John Mueller said. It doesn't matter if Google found it via HTML or JavaScript, assuming Google can find it.
Here is what John Mueller said:
Google will use the most restrictive setting you have on the page (this matches how robots meta tags are generally processed, eg if you have a "noindex" + "index", then the "noindex" will override the "index"). If you have a "nofollow" in static HTML and remove it with JS, Google will still use the "nofollow". Similarly, if you don't have any robots meta tag, and add a "noindex" with JS, Google will use the "noindex". In short, adding a "nofollow" via JS would work, removing it won't.
See that last line? If you add a nofollow using JavaScript, Google would honor that. If you add JavaScript to remove the nofollow from the code, Google won't honor that.
Google goes with the most restrictive signal they get here.