You can optimize JavaScript in the following ways:
Use external and not internal JavaScript.
<script src="url_to_js" type="text/javascript"/>
External JavaScript files can be cached independently of HTML
files, which is good because JavaScript is usually more static than HTML.
External JavaScript can be shared between pages. Remember to set the Expires header.
Minimize the size of the final output JavaScript code, for example with automation tools. Strip comments from your code. Strip excess white space. Minimize and pack the code, using GZIP compression. Remember to consider the good balance of the XHTML structure and unobtrusive JavaScript.
Do not run long-running JavaScript functions on page load. Web applications do not multi-task and the browser is thus blocked from the user input for the duration. If you have a long running script, try to speed it up, for example by profiling the functions and improving the XHTML structure.
Do not use JavaScript where CSS can be used instead. For example, implement menus and rollover images with CSS instead of JavaScript.