Gary Illyes from Google said on Twitter it is probably not a good idea to use JavaScript redirects. This was specific to redirecting users based on their location to a country specific version of the page. But Google has said before that server side redirects are the best.
Here is Gary's tweet:
Js redirects are probably not a good idea though.
— Gary öLÀí£¯?? Illyes (@methode) July 8, 2020
In 2016, John Mueller said on Google+ (trust me, there was a Google+) "JavaScript redirects: If you can¡¯t do a server-side redirect, using JavaScript is a good fall-back. If you¡¯re using a JavaScript framework for your site, this might also be the only option. Caching depends on the server settings, and search engines have to guess at what you¡¯re trying to do (index under R or S?)."
In 2010, John also told people to shy away from using meta refresh-type or JavaScript redirects.
Go server side when it comes to implementing redirects.
Forum discussion at Twitter.