Correct way to implement JS code/libraries for User Interface

Hello, I installed Custom CSS app to add custom CSS code, but how to add custom JS code or libraries, in Nextcloud? Is there a way to add it in a custom theme, to avoid overwriting when Nextcloud updates?

Thank you