One thing that is useful to do before you start testing your JavaScript is to validate your HTML. There are a number of possible errors that you may get with your script if the HTML that the script is trying to work with is not valid. Just to give one example, ids are supposed to be unique but if you accidentally specify the same id more than once in your HTML then this may cause your JavaScript to not be able to interact with the correct part of the page.
Just about any invalid HTML may result in JavaScript that is supposed to interact with it to fail to function correctly in at least some browsers and so you should at least ensure that if there are any parts of your HTML that you decide to leave as invalid that they are not parts of your page that is expected to interact with JavaScript. Regardless of such a decision, if your JavaScript fails to work correctly you should at least temporarily remove any invalid HTML from the web page in order to eliminate that as the cause of the error.
If you have any JavaScript embedded directly into the HTML then the JavaScript itself may cause validation errors when you validate your HTML. The best solution to this is to not embed any JavaScript into the HTML in the first place and restrict the JavaScript references in the HTML to script tags either in the head of the page or at the bottom of the body. Any JavaScript that can be embedded into the page itself can be easily moved into a JavaScript file instead.
The reason why your JavaScript may cause the HTML validator to report invalid code is simply that JavaScript isn't HTML and the validator is trying to validate HTML. If you include JavaScript inside the HTML then you do not have just HTML to validate and any parts of the content that are JavaScript instead of HTML may cause validation errors - particularly if the JavaScript actually contains references to HTML inside of document.write() or innerHTML references.
That the JavaScript can confuse the HTML validator can also mean that the HTML inside your embedded JavaScript may potentially confuse things as to just where the JavaScript itself ends since as far as JavaScript is concerned any embedded code that commences with a script tag ends as soon as the first >/ immediately following that script tag is found. If such a character combination exists within the JavaScript itself then this can be the direct cause of JavaScript errors. Even when using such code in external JavaScript it is advisable to escape this particular character combination as >\/ so as to ensure that it is not misinterpreted as the start of the tag closing off the script. Even better is to avoid the use of direct HTML code inside your JavaScript and to use the appropriate DOM commands to reference the HTML instead so as to eliminate any possibility of confusion.
Validating your HTML is a quick and easy way of eliminating possible errors in your JavaScript processing right from the start since by doing so you eliminate any possibility of any JavaScript errors being caused by invalid HTML. You also reduce the possibility of the script working in some browsers and not others since with valid HTML you know exactly what to expect there to be in the DOM and so all of your HTML access from the JavaScript is interacting with a known document object model. You don't know for certain what any particular browser will do with regards what it does or doesn't add to the DOM when it comes across invalid HTML and even if some given invalid HTML doesn't cause you any issues in all the most common browsers is no guarantee that your JavaScript will work correctly in less common browsers. Since you are not going to have the time to test your JavaScript in all the thousands of different browsers that exist you will want the HTML to be valid so as to reduce the number of browsers where your script doesn't work to those that do not implement HTML or JavaScript properly (hopefully very few or none) rather than having the script not work in those browsers that do not process invalid HTML in the way you expect (likely to be a higher number of browsers and to therefore affect more people).
The HTML is the foundation upon which your JavaScript is built and having valid HTML will make testing your JavaScript easier as well as eliminating possible causes of the script not working correctly.
