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

jslint and typeof

615 views
Skip to first unread message

Andrew Poulos

unread,
May 20, 2013, 5:59:52 AM5/20/13
to
A fragment of some code I've been given has this

if (typeof options.actor !== "undefined") {
actor = options.actor;
} else if (this.actor !== null) {
actor = this.actor;
}

and jslint complains of the first line that

"Unexpected 'typeof'. Use '===' to compare directly with undefined."

but the complaint makes little sense to me.

Why is typeof unexpected?
Why is it asking compare the string return with a primitive?

Andrew Poulos

Jukka K. Korpela

unread,
May 20, 2013, 6:40:31 AM5/20/13
to
2013-05-20 12:59, Andrew Poulos wrote:

> if (typeof options.actor !== "undefined") {
[...]
> and jslint complains of the first line that
>
> "Unexpected 'typeof'. Use '===' to compare directly with undefined."
>
> but the complaint makes little sense to me.

Since the only value of type "undefined" is the one denoted by the
variable undefined, the test

typeof options.actor !== "undefined"

is unnecessary complicated, since you can use

options.action !== undefined

instead.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

Eric Bednarz

unread,
May 20, 2013, 7:34:33 AM5/20/13
to
"Jukka K. Korpela" <jkor...@cs.tut.fi> writes:

> 2013-05-20 12:59, Andrew Poulos wrote:
>
>> if (typeof options.actor !== "undefined") {
> [...]
>> and jslint complains of the first line that
>>
>> "Unexpected 'typeof'. Use '===' to compare directly with undefined."
>>
>> but the complaint makes little sense to me.

OK.

> Since the only value of type "undefined" is the one denoted by the
> variable undefined,

Property, not variable, and that's not necessarily true, either.

> the test
>
> typeof options.actor !== "undefined"
>
> is unnecessary complicated, since you can use
>
> options.action !== undefined

This, however, is unnecessarily brittle since the undefined property of
the global object is writable in ES < 5. It is also overly verbose.

options.action !== void 0;

(JSLint won't like that either, of course.)

Thomas 'PointedEars' Lahn

unread,
May 20, 2013, 7:48:28 AM5/20/13
to
Andrew Poulos wrote:

> A fragment of some code I've been given has this
>
> if (typeof options.actor !== "undefined") {
> actor = options.actor;
> } else if (this.actor !== null) {
> actor = this.actor;
> }
>
> and jslint complains of the first line that
>
> "Unexpected 'typeof'. Use '===' to compare directly with undefined."
>
> but the complaint makes little sense to me.
>
> Why is typeof unexpected?

Probably because Douglas Crockford is wrong. Prior to ECMAScript Ed. 5,
when jslint was released, the “undefined” property of the Global Object was
already writable, and it was safer to compare against the result of the
“typeof” operation. Nowadays, and especially with host objects, there are
insufficient good reasons to choose a different approach.

> Why is it asking compare the string return with a primitive?

Crockford suggests that you write

if (options.actor !== undefined) {

instead. But this can only be recommended if you can assume that
“undefined” is read-only.

Ignore jslint.

That said, this should probably be written

var actor = options.actor || this.actor;

See also jsx.array.createComparator().

--
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

unread,
May 20, 2013, 8:23:47 AM5/20/13
to
Eric Bednarz wrote:

> "Jukka K. Korpela" <jkor...@cs.tut.fi> writes:
>> the test
>>
>> typeof options.actor !== "undefined"
>>
>> is unnecessary complicated, since you can use
>>
>> options.action !== undefined
>
> This, however, is unnecessarily brittle since the undefined property of
> the global object is writable in ES < 5.

ACK. And in earlier implementations there was no “undefined” property,
causing this to throw (the equivalent of) a ReferenceError.

> It is also overly verbose.

In what way?

> options.action !== void 0;

I would call *that* overly verbose. For backwards compatibility, I have
occasionally declared

var undefined;

However, nowadays the “!==” and “===” operators and the “undefined” property
of the Global Object can generally be considered safe language features.
Only a caveat with the property value and host objects must remain for the
time being.

Stefan Weiss

unread,
May 20, 2013, 8:44:13 AM5/20/13
to
On 2013-05-20 11:59, Andrew Poulos wrote:
> "Unexpected 'typeof'. Use '===' to compare directly with undefined."

Deja vu:
https://groups.google.com/d/topic/comp.lang.javascript/tJuCNzQEbb4

- stefan

Andrew Poulos

unread,
May 20, 2013, 9:17:10 AM5/20/13
to
Dang, I asked the identical question 4 months ago (which was duly
answered). It was only 8pm local time when I posted so I can't use the
excuse of lack of sleep.

Andrew Poulos

Eric Bednarz

unread,
May 26, 2013, 11:40:58 AM5/26/13
to
Thomas 'PointedEars' Lahn <Point...@web.de> writes:

> Eric Bednarz wrote:
>
>> "Jukka K. Korpela" <jkor...@cs.tut.fi> writes:

>>> the test
>>>
>>> typeof options.actor !== "undefined"
>>>
>>> is unnecessary complicated, since you can use
>>>
>>> options.action !== undefined

>> It is also overly verbose.
>
> In what way?

In the three-characters-more way (as far as I am concerned, the typeof
operator is safe to use and easy to read, so I was just being
sarcastic).

>> options.action !== void 0;
>
> I would call *that* overly verbose.

I don't, I just call it unreadable. :-)
0 new messages