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
Sep 19, 2018

Arr Matey, Hear a Tale about Cyber-Piracy

It’s Talk Like A Pirate Day.
Read more
Sep 18, 2018

The App Is The Endpoint

Traditional Endpoint Security is dead, that is to say that hardening the laptop, desktop, or device is not a panacea. ...
Read more
Aug 29, 2018

Using real-time threat analytics to thwart a serial app attacker

How Arxan helped shut down continuous reverse engineering attacks Operating in the Dark It started after releasing an app u ...
Read more