One common cause of errors in JavaScript is timing - the code would do the right thing if it were run at the right time.This particular sort of error doesn't exist for server side languages since there the script isn't interacting directly with the web page.
Most JavaScript does interact with the web page and so what parts of the web page are or are not loaded at the time your code runs will make a big difference to whether it will work or not. Timing issues with JavaScript have always existed but while early JavaScript could have issues with scripts running too late to do what you require, the most common timing issues now are scripts ruunning too soon to be able to do what is required.
The only way that early JavaScripts could interact with your web page was to use document.write statements to write the output of the script into the web page. These calls have to run before the page finishes loading since the web page calls an implicit document.close once the page finishes loading and so any further document.write calls after that will call an implicit document.open first which has the effect of creating a new empty web page for the subsequent document.write statements to write into. Of course anything that is done prior to the web page finishing loading can also be done by server side scripts and so most uses for document.write have become supurfluous since server side scripting became possible.
Most JavaScript now interacts with the web page using either innerHTML or the standard DOM commands. In both cases you generally identify where in the web page that you want the update to be applied to by using document.getElementById or some other DOM call to provide JavaScript with a reference to the place or places in the HTML that the script is to update. This means that the HTML that is to be updated must have loaded into the browser before the code to reference it runs as otherwise the script will fail to find the place to be updated.
Having JavaScript that tries to use document.getElementById('myid').innerHTML to update some content in your web page fail to work even though the <div id="myid"> is right there in your HTML where you expect it to be is possibly the most common modern JavaScript timing error.
Before you can do this you must first identify that it is a timing error and the easiest way to do that is to add an alert immediately before the line of code that isn't working correctly. The alert will interrupt the loading of the web page and so you will have a visual clue as to how much of the page has actually loaded at that point. If your code is running in the head of the page then the web page will be completely blank since none of the body will have started loading yet. If your script is running within the body of the page then everything in the page to that point will have loaded (although all of the images that are referenced may not have finished downloading yet). If the entire web page is there then the ascript is either running as the very last thing in the page or has been triggered by the window.onload event.
The solution to all JavaScript timing errors is of course to adjust when the script runs. With most modern JavaScript code timing errors are usually due to the code running too soon in which case moving the script to the end of the body of the page or enclosing the code in a function and calling it from the onload event will generally fix the timing error. For document.write timing errors the solution is to replace the document.write statement either with innerHTML or DOM calls so that you update the content of the current page rather than starting to create a new page.
