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
May 07, 2018

It's Time To Get Serious About Application Security

Apr 02, 2018

How to Detect App Threats to Protect Your Business

Apr 02, 2018

Protecting Apps Is Not Enough: Why You Need Threat Analytics

Every app downloaded via an app store is running in a
Read more