Just like CSS, and for the same reasons, we often "move" our JavaScript code to an external file (typically named something like script.js), then we include this link in our HTML file:
<script type="text/javascript" src="script.js"></script>
More on External JavaScript