6 JavaScript Best Practices for Beginners

As you gain experience working with a particular language, there are certain practices you will learn to use that constitute labeling your work as “good” code as opposed to messy or bad. These practices will always differ depending on the language you’re working with.

Here, we’re going to talk about the best practices that you should be using when writing code with JavaScript. Obviously, this also applies to jQuery, as well.

1) Use the Right Equality Operator

In JavaScript, there are two different kinds of equality operators you can use.

They are “===|!==” and “==|!=”.

The best practice is to use the former equality operator (“===!|==”) when comparing values.

2) Avoid Using the Eval Function

If you use the eval function in JavaScript, you can access the compiler. This happens when you execute a string’s result by passing it as a parameter of the “eval” function.

You should not use this method for several reasons. First, it will decrease the performance of your script and your page loading times will take a hit. Second, it creates a significant security risk because you are providing the string with way too much power.

Avoid using the eval function at all costs.

3) Always Place Scripts at the Bottom of Your Page

It is considered best practice to add any and all scripts to the bottom of your page. There is a logical reason for this. When loading a script, web browsers will stop there and will not continue until the entire file has been loaded. This means that the end user will have to wait longer for their browser to go anywhere, giving off the impression that the page isn’t doing anything.

That’s why it’s a good idea to place your scripts at the bottom of your page, so the rest of your content can load before the scripts are executed.

Generally, you want to place your scripts just above the closing body tag in your HTML file.

4) Always Check Your Script with JS Lint

Most languages have a debugging tool that will highlight errors and problematic lines of code. JS Lint is that tool for JavaScript.

It is an online debugger, written by Douglas Crockford. Anyone who works with JavaScript knows what this tool is, and most likely utilizes it regularly. You should get in the habit of using it too.

Once you have written your script, copy and paste it into the JS Lint tool. It will scan for problems with your code and flag them for you.

5) Always Use Comments to Document Your Methods

This is considered a best practice of any language. Always leave a comment to document important sections of your code.

When you have to sort back through your written code to find errors or specific functions you’ll thank yourself for following it.

Comments can be created in JavaScript by simply adding “//” in front of the message.

6) Don’t Forget to Use Semicolons

A majority of modern browsers will let you slip by even if you forget to use semi-colons in your code. Do not get lazy and fall in the habit of doing this often. It is a very bad practice, and will lead to significant problems later on. Worse yet, it can be extremely hard to find a problem you’ve created by not using the proper semi-colon tags.

Don’t forget to use them, they exist for a reason.

 

For the final segment of our tutorial, we’ll take a look at some additional resources you can use to learn more about jQuery and JavaScript.

Recommended Training – Treehouse

TreehouseAlthough this site recommends various training services, our top recommendation is Treehouse.

Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises.

Treehouse's mission is to bring technology education to those who can't get it, and is committed to helping its students find jobs. If you're looking to turn coding into your career, you should consider Treehouse.

Read our full review of Treehouse…



Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.