How do I add JavaScript?
Your canonical reference for this is here. In short:


Save the CSS or JS to a file. (I normally do this at
docroot/themes/projecttheme_directory/js/)
Define a "library", which can contain both CSS and JS files.
"Attach" the library to a render array in a hook.


You can add these files globally, (see "Attaching to all pages") or to specific pages or blocks, (Attaching a library in a Twig template) There's a good deal of flexibility in how you can add your javascript, so be sure to review all the options listed there.