Congratulations, you've learned the basics of programming! That's wonderful, but you'd better not go out into the world and write crappy code. Before we set you free, here are some best practices and good things to keep in mind.

Best Practices

Comment Your Code and Comment It Well

You can comment your code in two ways: one way for single-line comments and one way for multi-line comments. Single-line comments start with // in JavaScript. Other languages use other characters, like the # sign, so be sure to check before you start putting forward slashes everywhere. To make a multi-line comment, you just put your comment in between /* and */. Here's an example of both:



// Single-line comment
/* Multi-line comment
Another line
One more! */

You'll mostly use single-line comments for making actually comments about your code and use multi-line comments to remove parts of your code without deleting them. This makes it easier to isolate a problem when you're debugging.

So how do you write good comments? Well, you're probably going to feel inclined in the beginning to write comments that explain how everything works. If that helps you remember, that's not a bad thing to do in the beginning. For the most part, however, you're going to be naming your functions and variables clearly and you won't need to explain how something works or what a particular function does because that clarity already exists. What you want to explain in your comments is information the code can't already tell you: why you made the choices you made. Anything that will help another programmer (or yourself, when you look back at this code months/years later) better understand your code is worth putting in a comment. If it's redundant information, it's not helpful. If it helps bring clarity to the code you've written, it is.

Don't Use the eval() Function

Oh boy, the eval() function isn't the easiest thing to explain. If this explanation doesn't make enough sense and the video doesn't get the idea across either, just remember not to use eval() whenever possible. The main reason is that eval() slows down your code. The other main reason is that you can almost always find a better way to get the job done than to use eval().


So what does eval() do? Well, it's short for evaluate and it evaluates a string as if it were a variable. So let's say you had a variable called numberOfApples and wanted to alert it to a user. You could do it like this:


What if you put numberOfApples in quotes? Well, the alert wouldn't alert whatever number you set numberOfApples to, but instead just alert the text "numberOfApples". Eval solves that problem:




Why would you ever use this? Well, someday you'll probably find a reason why eval() will be useful to you. Generally it's because you don't necessarily know the name of the variable, or the name of the variable you need is contained in another variable. It gets complicated and we're certainly not going to get into it here, so just remember: don't resort to using eval() unless there is no other way—and I can't think of a situation where you wouldn't have an alternative.

Create Arrays and Objects Faster

We didn't really talk much about objects but you should definitely remember arrays from Lesson 3. When we created them earlier, we did it like this:

var myArray = new Array("item1", "item2", "etc");

You don't really need the new Array statement, however, as you can just do it like this:


var myArray = ["item1", "item2", "etc"];

You also have a similar shortcut with objects, a type of variable we haven't discussed yet. Objects are very similar to arrays in that they can hold more than one piece of data at a time, but they're accessed a little differently and you can easily name each item in your object. It's really easy to see the difference when you look at the long way of creating an object:


var myObject = new Object();
myObject.item01 = "Ponies";
myObject.item02 = "Unicorns";
myObject.item03 = "Rainbows";

The long way works fine, but here's an easier way to do it:


var myObject = { item01: "Ponies; item02: "Unicorns"; item03: "Rainbows"; }

These shortcuts make writing your code a bit faster and make your code a lot easier to read.

Use Your Semicolons!

JavaScript does not always requires you to end a statement with a semicolon, but you should do it anyway. We discussed this a little bit in Lesson 1, but it's worth repeating. You're almost guaranteed to run into problems if you're careless about using your semicolons, so whenever you need to end a statement (a sentence of code), don't forget to drop a ; at the end.

Additional Resources

Now that you've learned the basics, hopefully you want to learn more and start making some awesome programs. Here are a few additional resources to help you learn more JavaScript as well as some other languages.

DO NOT Use W3Schools

When you search for help online, one of the first results is often W3Schools (which I'm explicitly not linking to here). The short version is that it sucks. It's full of errors, it's missing information, and while it's not 100% useless it isn't a good resource. Avoid it. For the long version, visit W3Fools, a site put together by the jQuery team (and some other helpers).

General Resources



  • The official PHP Manual is how I learned PHP. I just searched for functions, browsed around, and learned by example. PHP was also the first language I learned, so it's not as if I knew what I was doing. The manual is very informative and has great examples. While I'd probably recommend learning programming basics before diving right in, you just did in this series so playing around with PHP shouldn't be too tough for you.
  • Zend offers up PHP for the Absolute Beginner. Zend is a company, but also a powerful PHP framework. If you want to start from step zero (which isn't a bad idea), check it out.


ActionScript (Flash/Cross-Platform AIR Apps)

  • gotoAndLearn is filled with excellent tutorials that teach you how to make really useful stuff in ActionScript, whether you're using Flash or Flex to deploy to the desktop or web. One of the best ways to learn is by doing, and gotoAndLearn offers plenty of opportunities to do just that.

Mobile App Development

Special thanks to my friend Colin Snover for his input. He currently works on jQuery and is much smarter than me. Follow him on Twitter. Also, a special thanks to CnEY?! for a few of the resources listed here and making sure I had nothing good to say about W3Schools.




(function(){var w = window,amznAsin = (w.amznAsin && w.amznAsin.constructor === Object) ? w.amznAsin : {};amznAsin["0596101996"] = "0596101996";w.amznAsin = amznAsin;})();