Whether you are just starting out with JavaScript or have been writing JavaScript for some time you may not be coding your JavaScript in the best way. Ideally your JavaScript should be completely unobtrusive with no JavaScript at all apprearing in the HTML of the web page and only one, or perhaps two, script tags to link the JavaScript to the HTML. By coding your JavaScript this way you gain a number of benefits over what you get if you mix the JavaScript and HTML together in the one file. There are also a number of other aspects to the way that you write your JavaScript that will help you to make it so that the individual scripts you add to your page are less likely to interfere with each other and more likely to work in all the different browsers.
At this point I am going to assume that you have already reached the decision that you want to write your JavaScript the modern unobtrusive way and we will consider aspects of how to actually do it. I am also going to assume that you know at least the basics of JavaScript including what variables and operators are, how JavaScript uses if and case statements to make decisions, and how to separate up your code using functions so if you are just learning JavaScript you should work through my tutorials on those topics first.
Just about all tutorials on programming start with the simple code to display "Hello World" in your web page. That tutorial serves to introduce you to the language and in this particular case demonstrates how to attach JavaScript to a web page in a modern unobtrusive fashion.
JavaScript is an Event driven language and so little of the JavaScript we add to the page is actually going to run except when triggered by an event. There are a number of different types of event that can trigger our code to run including Visitor Triggered Events and Timed Events.
The most efficient code is that where you use all of the features of the language where they are appropriate and many people skip over some parts of a language when they are learning it particularly in the early stages where there is so much to learn. Many people don't use all of the JavaScript ConditionTesting options, particularly the tests for identically equal and its extension into using switch/case statements instead of if statements. There are also people who still use browser sensing which was never necessary in JavaScript rather than the far more accurate Feature Sensing which allows you to determine whether the browser supports the code you are trying to use rather than your needing to know which of the many thousands of browsers actually do support it.
When you place the script tag in the head of the web page you need to perform event handling and test a variety of conditions in order to determine when your code can actually be run if it is dependent on the content of the page. There is an Alternate Tag Location that may simplify your testing as the script will run after the HTML has loaded rather than before.

