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...
variable === undefined VS typeof variable === “undefined” Here the assigned variables don't have any value but the variable exists. Here the type of variable is undefined.
The typeof undefined is the string "undefined" — and undefined is a falsy value that is loosely equal to null but not to other falsy values.
In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.
In modern browsers you can safely compare the variable directly to undefined : if (name === undefined) {...} After that re-assignment, comparing with undefined directly would no longer correctly detect whether a variable was assigned a value. The value of undefined is undefined (see 8.1).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With