Hello,
1) Read more about child theme and how to make changes there https://developer.wordpress.org/themes/advanced-topics/child-themes/
2) Use Insert Header and Footers plugin to add JS code in the header or footer. Then you won’t lose it if you decide to change theme.
Regards