Posts tagged with 'double-equals'

JavaScript: an acceptable use of double-equals? Just.

When I first started learning and writing JavaScript, the recommendation I came across again and again was that the equality operator, ==, or “double-equals” was the devil incarnate. You should always use the identity operator, ===, or “triple-equals” because then you won’t have to battle weird type coercion bugs (and remember it’s not necessarily the time you write the original code that you’ll get tripped up, it’s the time you maintain it for the nth time).

Upside Downphoto © 2008 Nick Farr | more info (via: Wylio)So, imagine my surprise when reading the jQuery library code, I came across several uses of double-equals. What? Have Resig and the gang lost it?

Further investigation showed me the pattern they were using. In all cases, when double equals is used, the comparison is checking against null:

if (someObj == null) {
  // do something
}

OK, so is comparing against null somehow special? Time to refer to the language specification.

Section 11.9.3 of the 5th Edition gives the long-winded answer as a descriptive flowchart. In essence, the rules for when one side is null are as follows:

  • if both sides are null, return true;
  • if one side is undefined and the other is null, return true;
  • otherwise, return false

Note that this is more restrictive than just writing:

if (someObj) {
  // do something
}

since this also allows the other four falsy values (false, 0, NaN, "") to pass through.

And it’s also simpler than writing:

if ((someObj === undefined) || (someObj === null))  {
  // do something
}

which is what it means precisely.

But, man, reading code that uses this construction brings me up short every time. It just seems to be a violation of best practices.

Album cover for HearsayNow playing:
O'Neal, Alexander - Criticize
(from Hearsay)


Extras

Search

About Me

I'm Julian M Bucknall, the M because it's my middle initial and because I and the other Julian Bucknall (the movie guy) would like to differentiate ourselves.

I'm a programmer by trade, an actor by ambition, and an algorithms guy by osmosis. I write articles for PCPlus in my spare time, not that there's much of that.

Julian M Bucknall Apart from that, an ex-pat Brit, atheist, microbrew enthusiast, Pet Shop Boys fanboy, slide rule and HP calculator collector, amateur photographer, Altoids muncher.

DevExpress

I'm Chief Technology Officer at Developer Express, a software company that writes some great controls and tools for .NET and Delphi. I'm responsible for the technology oversight and vision of the company.

Validation

Validate markup as HTML5 (beta)     Validate CSS

Bottom swirl

Archives

May 2012 (4)
SMTWTFS
« Apr  
12345
6789101112
13141516171819
20212223242526
2728293031

Like this Archive Calendar widget? Download it here.

Social networking

The OUT Campaign

The OUT Campaign

My Tweets

  • Honest Movie Trailer of Phantom Menace http://t.co/sif8y4Ns and then Battleship, er, Transformers http://t.co/sif8y4Ns
  • Damn, Donna Summer and Chuck Brown both gone in the last 24 hours. Different types of music, sure, but enjoyed them both. :(
  • Just saw a company page showing a list of tweets with "Join the conversation" linked to their Twitter a/c. The tweets are 6 months old #fail
Bottom swirl