Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

typeof and jslint

169 views
Skip to first unread message

Andrew Poulos

unread,
Jan 5, 2013, 6:17:29 PM1/5/13
to
If I test the following with jslint
if (typeof num === 'undefined') { return false; }

I get the error
Unexpected 'typeof'. Use '===' to compare directly with undefined.

I'm not sure what its telling me. Is it saying that I should instead use
if (num === undefined) { return false; }
?

Why is tyepof unexpected?

Andrew Poulos
Message has been deleted

Andrew Poulos

unread,
Jan 5, 2013, 7:04:01 PM1/5/13
to
On 6/01/2013 10:40 AM, Luc Yen wrote:
> Andrew Poulos於 2013年1月6日星期日UTC+8上午7時17分29秒寫道:
>> If I test the following with jslint
>> if (typeof num === 'undefined') { return false; }
>> I get the error
>> Unexpected 'typeof'. Use '===' to compare directly with undefined.
> [...]
>> Why is tyepof unexpected?
>
> $ jslint --browser filename.js

Is that an answer to my questions? If it is, I don't understand it.

Andrew Poulos


David Mark

unread,
Jan 5, 2013, 7:16:29 PM1/5/13
to
Because Crockford thinks you should compare to the undefined value.
That's all.

Of course, that's not typically the way it is done for host object
detection. Theoretically using the strict comparison would avoid at
least some of the known pitfalls, but typeof has always served me well
in discovering host objects and their methods. I agree that it makes
sense to avoid typeof for native comparisons. Many years ago typeof
was used to avoid /undefined/ out of compatibility concerns, but that
issue is long dead.

Stefan Weiss

unread,
Jan 5, 2013, 7:21:07 PM1/5/13
to
On 2013-01-06 00:17, Andrew Poulos wrote:
> If I test the following with jslint
> if (typeof num === 'undefined') { return false; }
>
> I get the error
> Unexpected 'typeof'. Use '===' to compare directly with undefined.

That's just plain wrong. "num === undefined" may do the same thing, but
only if num has been declared or can be otherwise resolved from the
current scope. If you can't be sure that num exists, typeof is mandatory.

This is bad advice, and yet another reason not to use JsLint. You could
try to report this on the jslint_com mailing list, and hope that DC
agrees with you. Or you could use another linting tool like JsHint.


- stefan


PS: In addition to the above, the 'undefined' identifer is not
guaranteed to evaluate to the undefined type, but that's more of a
theoretical concern. Your original code would be safe from that.

Thomas 'PointedEars' Lahn

unread,
Jan 5, 2013, 7:21:58 PM1/5/13
to
Andrew Poulos wrote:

> If I test the following with jslint
> if (typeof num === 'undefined') { return false; }
>
> I get the error
> Unexpected 'typeof'. Use '===' to compare directly with undefined.
>
> I'm not sure what its telling me. Is it saying that I should instead use
> if (num === undefined) { return false; }
> ?

Probably, yes.

> Why is tyepof unexpected?

Because Douglas Crockford has some rather weird and purely subjective
notions how these languages – that he misleads others to think of as a
single one – should be written, and those notions are expressed and
enforced by jslint.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Luc Yen

unread,
Jan 5, 2013, 7:33:44 PM1/5/13
to
Andrew Poulos於 2013年1月6日星期日UTC+8上午8時04分01秒寫道:
jslint enforces it's coding style. The following snippet is ok.
The typeof line is identical to the original code.
-----
var num;
function test() {
"use strict";

Eric Bednarz

unread,
Jan 10, 2013, 6:53:31 PM1/10/13
to
David Mark <dmark....@gmail.com> writes:

> [...] Many years ago typeof
> was used to avoid /undefined/ out of compatibility concerns, but that
> issue is long dead.

Between that and ES5, `undefined' was writable.
0 new messages