Beginner Series: jQuery Tutorial
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.
1) Use the Right Equality Operator
They are “===|!==” and “==|!=”.
The best practice is to use the former equality operator (“===!|==”) when comparing values.
2) Avoid Using 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
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.
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.
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.