pytrin is correct, in a live environment keep your JavaScript code in a separate file.
I'd like to add two things...
1.) While it is valid to have a script element within the body element I consider it unprofessional.
2.) The third example you posted is how you should do it for test cases when you want to edit a single file to test a script out on the same page you are executing the script on.
Douglas Crockford wrote: JavaScript programs should be stored in and delivered as .js files.
JavaScript code should not be embedded in HTML files unless the code is specific to a single session. Code in HTML adds significantly to pageweight with no opportunity for mitigation by caching and compression.
<script src=filename.js> tags should be placed as late in the body as possible. This reduces the effects of delays imposed by script loading on other page components. There is no need to use the language or type attributes. It is the server, not the script tag, that determines the MIME type.