1. Technology

How I Write JavaScript

By

While not everyone writes their JavaScript the same way, knowing the steps that I usually follow in writing a script may assist you in working out the approach to writing JavaScript that will work best for you.

The very first thing that I do before I start writing any JavaScript is to make sure that the HTML that the JavaScript is going to interact with is in place. If the script is going to be updating CSS then I also make sure that the CSS is in place and working.

The next step is to add a script tag to the web page that I can add the JavaScript into. While it is preferable to have the final JavaScript in a completely separate file, it makes it easier to write and test the JavaScript with it actually in the page.That helps avoid any issues of the JavaScript file getting cached and your not being able to test if a change you just made worked.

The next step is to create a function for each of the actions that the script is to perform and attach them to the appropriate HTML so that they will be called when the event that is to trigger that action occurs. Usually I'll just stick an alert in each to start with just to give me a way to check that each function gets called at the appropriate time. I usually test the script in at least one browser a number of times as I progressively write the script so as to ensure that each piece works correctly before moving on to write the next piece. Where the script involves code that will work differently in different browsers I'll do at least some testing along the way in additional browsers so as to test each path through that part of the code.

Now it is time to start writing the actual JavaScript for the functions. I start by working out what a particular function needs to do and I add any code to the script that I have already written that will provide any part of that processing. As I write more and more scripts the opportunity to reuse code that I have written previously means that there will be less and less that I will need to actually write from scratch for the new script.

At this point I concentrate on getting the code to function correctly when valid information is input to it.Where the script is going to be processing information from a form I leave adding the validations of those fields until after I get the code working correctly for valid input. It is only after I have the script working correctly with valid input that I then look at adding all of the extra code necessary to validate whatever has been fed into the script in the first place. Often this validation code will be as long or longer than the code that actually does the processing but as I can usually copy validation code that I have used previously in other scripts, adding this part of the code is usually much quicker than writing the main functioning of the script. Where the script doesn't rely on any user input there may not even be a need to add any actual validations.

With the script working I now review all of the code that I have used in order to see if I can find anything I have overlooked or where a change could be made to simplify the required code. Where I decide to rewrite sections of the code after I get the script working I generally keep a copy of the original working version so that if the modification doesn't work out I can revert back to the prior working version easily and quickly.Sometimes in order to get started more quickly into writing the main sections of the script, I'll use a more obtrusive way of attaching the script into the page to start with (such as placing event handlers directly in the HTML) and so one thing that I would then do at this point is to replace those with less obtrusive code within the JavaScript itself.

Once the script is finished and cleaned up I then move the script out of the web page into its own separate file and update the script tag to reference that file. The script is now ready to upload to the web.

  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. How I Write JavaScript

©2014 About.com. All rights reserved.