Skip to main content
May 25, 2011

More Javascript and jQuery Mistakes

Continuing my last post, here are some more Javascript and jQuery mistakes that you should avoid.

These are common mistakes that can be tricky even for seasoned developers. Javascript is a language with a greater-than-average number of quirks and becoming familiar with them can make your life easier.

Trailing Commas


You must remove trailing commas from arrays and JSON, such as:

var foo = {"name" : "James", "score" : 150, }; // BAD

This code may work on some browsers, but will fail in older and less-compatible browsers. You must always remove trailing commas to be safe:

var foo = {"name" : "James", "score" : 150}; // GOOD


Date Object

The Javascript date object has some weird functionality regarding months. The Date.getDate() command returns the day of the month from 1 to 31, as you would expect. However, the Date.getMonth() function returns the month from 0 to 11. Similarly, the Date.getDay() is from 0 to 6. Don't be mistaken- 1/11 is February 11, not January 11.


Double vs. Triple Equals

'==' and '===' have one important difference- triple equals tests for type as well as equality. For instance, (0 == false) evaluates to true, and (0 === false) evaluates to false, because the objects are of different type. It's good practice to always use triple equals to ensure consistency and guarantee that you aren't doing implicit type conversion that may fail on certain browsers.


Not Using jQuery

When is the right time not to use jQuery? When you are doing something simple, or have a highly optimized string of code that needs to execute quickly. You need to test this code thoroughly in all browsers you support. There are other JS libraries out there, but none of them are as capable and supported. If you want to manipulate the DOM, use jQuery and don't stress about bugs!

Avoid these mistakes and be sure that your code is clean and bug-free.

Apperian

More from the Blog
Feb 20, 2019

Part 4: App Security Should Be An Integral Part Of Your DevSecOps Process — Not An Afterthought

How Arxan can help streamline and optimize your DevSecOps process One of the most important factors to keep in mind when dep ...
Read more
Feb 13, 2019

Part 3: App Security Should Be An Integral Part Of Your DevSecOps Process — Not An Afterthought

Situations When DevSecOps Won’t Work Though DevSecOps is getting more popular by the day, and has many benefits to an organi ...
Read more
Feb 06, 2019

Part 2: App Security Should Be An Integral Part Of Your DevSecOps Process — Not an Afterthought

How to start implementing a DevSecOps process As you may have read in our
Read more