Think about what you want to do, and how you want to do it, before starting coding.
Build up a program in small steps. Write a bit of code, test it to see if it works, then write the next bit. If you write a large chunks of code, it may well have several mistakes in, and the combination of mistakes will be very difficult to find and correct. If using a feature that you haven't tried before, then write a small webpage to try it out first, before adding it to your main code. If you put it in a complicated program straight away and it doesn't work (which is quite likely), then you won't know if it's a simple typing error or that you've misunderstood how the code works.
Use copy-and-paste whenever you can. Mistakes can creep in whenever you type something, and it can be difficult to remember all the brackets and quotes. Copy something that you know works already and change it to do what you want. There are lots of pieces of this code in this website which you can copy.
Keep It Simple, Stupid (otherwise known as the KISS principle). If there is a simple way of doing things, and a complicated way of doing things, choose the simple way. Why make life harder than it is already?
Be careful of quotes. Make sure that you remember both the start and end quotes. If you have quotes inside quotes, they must be of different types (i.e. single and double quotes) although it doesn't matter which comes first. You can't have quotes inside quotes inside quotes!
Be careful of brackets. Make sure you have the right type. There are three types in JavaScript ( ) { } [ ] and one in HTML < >. Also make sure that you don't forget the closing bracket. It's a good idea always to type the open and close bracket at the same time, then move the print cursor inside them and type the contents. Missing brackets can cause a lot of trouble in JavaScript! Here is my debug page for curly brackets.
Be careful of infinite loops. Make sure the conditions of your loops will terminate. Don't forget to increment your counters!
Remember that in JavaScript, when you want to say "If this equals that", the code is "if (this==that)" and not "if (this=that)". The single "=" means "set this to that". Getting this wrong is a common bug.
JavaScript is case sensitive which means it notices whether a word is upper case, lower case or a mixture. Make sure that you get commands right (usually lower case). Decide which case to use for names of variables, and stick to it.
Use sensible names. It will make it easier to code your program, it will make it easier to read your program and this will make it easier to spot mistakes. Make sure that names of variables are not JavaScript or HTML commands.
Define all your variables, and make them global unless they are strictly temporary.
Lay out your code to make it easy to read. Use spaces, blank lines and new lines so the underlying logic is plain, and the blocks of code easy to distinguish.
Use comments especially at the start of functions, to explain what they do. Even if you've used a sensible function name, and even though you are convinced that the function is obvious, you'll probably forget all about it quite quickly, and will be thankfully for any hint you've left yourself. You might want to change the code some time in the future, for example. I've had to rewrite whole webpages because I couldn't figure out what I'd done in an old one!
Debugginging
You may need to do something to get debugging options. Press F12 (top of keyboard) in IE, Edge or Firefox. That may give a message. The message may be unhelpful!
Some errors happen because you've typed something wrong or made a silly mistake. (But it can be very, very hard to spot it later!) Some errors are fundamental to the logic of the program, and you've designed it wrong. But remember, when looking at the computer in bewilderment, saying "Why did it do that?", the answer always is "Because you told it to!"
When trying to find out why your program doesn't work, first check all your typing really carefully. Check all brackets and all quotes. Make sure that you're using the right sort of brackets. Check that your names are unique, and that they are not otherwise used by JavaScript or HTML. Check lower and upper case is correct. Check that variables are defined and global if necessary (so you can use a variable in more than one function). Check that a condition (within an if statment) of equality uses "==" not "=".
If you tend to make a particular type of error (such as upper/lower case problems) then check for those.
If you have an infinite loop, find out which loop it is, and check its condition.
Concatenation and plus. JavaScript uses "+" to mean plus if dealing with numbers, but concatenation (joining end to end) if dealing with strings. The problem is that often in JavaScript, you don't know whether something is a number or a string! If JavaScript meets "+" and it thinks that one of the tiems is a string, then it will concatenate even if you think they are both numbers. So 1+"2" is "12" not 3. If you think JavaScript is getting it wrong, use parseInt and parseFloat to force something to be a proper number.
If you are still stuck, try to work out exactly where the problem is by commenting out pieces of code. If this makes the problem go away, make the pieces of code you comment out smaller and smaller, until you've trapped the problem in a very small area.
If that doesn't work, try putting in the occasional alert box to see if variables contain what you think they should, or even if you've got to where you think you have in the code.However, do not put alert boxes in a piece of code which is actioned a large number of times. The alert will come up every time it is run, and you have to OK it each time, and you can't even close down the page, because you haven't OKed the alert, and when you do, another one appears.... In that situation, you could code dynamic HTML or a text box to display the information, since that doesn't require an OK.
Have a rest if your mind is getting fogged up. It's well known that computers give off magical rays which inhibit thinking! Well, perhaps not. But it's amazing what walking away from the computer for a bit will do. Have a cup of coffee. And there's something called fresh air...
If still no good, find someone sympathetic (preferably another programmer), and explain how your program should work and what happens instead, going into detail. Whenever I've got to this stage, I've spotted the problem before getting half-way through the explanation!