9

I'm confused with JSLint.

My code originally checked if div:jqmData("me") was undefined like so:

if ( typeof el.jqmData("me") == "undefined"  
   ? el.not(':jqmData(panel="main")').length > 0 
    : el.not(':jqmData(me="first")').length > 0 ){

}

JSLint complains that I should replace checking with typeof with ===, so I did like this:

if ( el.jqmData("me") === "undefined"  
   ? el.not(':jqmData(panel="main")').length > 0 
     : el.not(':jqmData(me="first")').length > 0 ){

}

JSLint doesn't complain anymore, but my nested if statement is broken, because I'm now always ending up with the 2nd if el.not(':jqmData(me="first")').length even when I should not.

Question:
Why does JSLint recommend === over typeof == undefined? How comes this breaks my logic?

Thanks for some enlightment...

frequent
  • 27,643
  • 59
  • 181
  • 333
  • 10
    `x === undefined`, not `x === 'undefined'` – zerkms Nov 02 '12 at 22:38
  • @zerksm: please make it an answer! – frequent Nov 02 '12 at 22:39
  • 4
    Depending on what you're checking, there's an advantage in using `typeof someVar === 'undefined'` over `someVar === undefined`, namely that the former won't break if `someVar` isn't, in fact, defined. For example, if `someVar` hasn't been declared, you'll get a `ReferenceError` using the latter, while the former works without a hitch. See: http://stackoverflow.com/questions/4725603/variable-undefined-vs-typeof-variable-undefined ( cc @zerkms ) – NullUserException Jan 09 '13 at 21:43
  • @NullUserException: yep, makes sense for checking variables (as an opposite to properties) – zerkms Jan 09 '13 at 22:05
  • Possible duplicate of [Why does jslint tell me to use ===?](http://stackoverflow.com/questions/12628068/why-does-jslint-tell-me-to-use) – Matthew Simoneau Dec 01 '15 at 03:25

2 Answers2

6

You've broken the comparison logic. It's assumed you use

typeof el.jqmData("me") === "undefined"  

or

el.jqmData("me") === undefined

Personally I'd go with the latter.

And personally I think that this particular JSLint check in this particular case makes not much sense.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • yeah. Agree. Thanks for clarification. Good I found this, I have been changing this aplenty... – frequent Nov 02 '12 at 22:54
  • 5
    But that doesn't work if you want to test that a variable is defined or not: (someundefinedvar === undefined) is a referenceError. Yet JSLint still complains about it even when I am testing variable def. – mattacular May 29 '13 at 15:56
  • @mattacular: yep, I missed that since I don't check that often – zerkms May 29 '13 at 20:26
0

What zerkms wrote is correct. An explanation can help though, from https://github.com/jamesallardice/jslint-error-explanations/issues/10#issuecomment-18273885:

A modernization of style has occurred with undefined comparison. ES5 guarantees that undefined is undefined. In strict mode, in comparison with the old and new styles, the typeof "undefined" check is longer to type and is no longer necessary now that undefined can be compared directly.

See the JSLint discussion: https://plus.google.com/101248256976407044060/posts/Q5oFnnxG9oL

Crockford basically says that typeof "undefined" check is longer and slower and is unnecessary.

Marc Durdin
  • 1,675
  • 2
  • 20
  • 27