JavaScript for Beginners: Programming Tips
- JavaScript Issues
- JavaScript for Beginners
- More Information
- Describe Your Goal
- Write down what you wish to accomplish. Then break it down into smaller parts. Try to find the simplest part, and implement just that part. If you can’t get that simple part to work, see if you can break it down into even simpler parts.
- Programming Tips: Test Often
- Learn to love your browser’s reload button. Whenever you make a change that might affect your web page, test immediately. Don’t make a whole bunch of changes before testing; if your web page no longer works, you won’t necessarily know which change caused it to fail. Write your programs a step at a time and test often.
- Write cleanly
- JavaScript, as a programming language, is much less forgiving than HTML or even XHTML. Commands end with semi-colons. Upper and lower case are important. Curly brackets enclose blocks of JavaScript code. You can’t play around with those rules and expect to get a working script.
- Find a good text editor
- A good text editor (I use Peter Borg’s Smultron for Mac OS X) will help you avoid mistakes. It will color code functions and keywords, as well as strings of text. It will automatically indent for you when you open and close curly brackets, and it will show you where a particular bracket or quote begins or ends.
- Centralize Your Code
- You should almost never repeat the same basic code more than once. If you need the same routine a second time, seriously consider moving it into a subroutine.
- Avoid the Hammer Syndrome
- Once you have a hammer, all problems start to look like nails. It can be easy, once you start programming JavaScript, to look at every web page problem as a problem with a JavaScript solution. Look for simpler HTML solutions first. HTML is faster and more reliable than JavaScript, and will last longer.