Your second example is attempting to reference a script from an external file called xxx.js in this case located in the same folder as the html file. If you created that file and placed the alert into that file and moved your script block into the head tag then you would find it would work.
Placing javascript in external files is an encouraged practice because it allows you to reuse common functions in many pages with a simple inlcude statement in your html. Plus it keeps your html files much cleaner.
When you start writing lots of javascript you can combine all your script into one file and then minify it using something like JSMin : http://www.crockford.com/javascript/jsmin.html
This compresses all your script into a tiny form that is unreadable to humans but is much quicker for your sites visitors because it means the script file is smaller and there is only one request to serve the file to the client.
On a side note, another helpful tool when writing javascript is JSLint.
It parses your javascript and informs you of syntax errors and also bad practices.
Happy Coding
Paul