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

typeof and strict equality

7 views
Skip to first unread message

mireero

unread,
Sep 25, 2016, 5:16:50 AM9/25/16
to
Hi,

This is more about loose/strict equality but typeof will be a good
example for the question.

When using typeof and comparison, I often see in others code strict
equality being used.
Is there any good reason for using "===" instead of "==" (apart from a
slight improve in performance, I once read a 4% faster for "===") ?

If I have:
typeof some_var;
This will always returns(*) a string like in:
console.log(typeof document);
=> "object"

If I do the comparison of typeof some_var against a string, there
shouldn't be any casting.

When someone wants to test if some variable is defined, I usually see:
if (typeof some_var === "undefined") {
Would I have the expected and identical behavior with:
if (typeof some_var == "undefined") {

I can't see why it wouldn't but as I keep reading "===", I thought I'd
better ask!


(*)
typeof is an operator, not a function.
Semantically, is it correct to say that typeof "returns" something ?

John Harris

unread,
Sep 25, 2016, 6:40:49 AM9/25/16
to
On Sun, 25 Sep 2016 11:16:42 +0200, mireero <mir...@free.fr> wrote:

<snip>
>(*)
>typeof is an operator, not a function.
>Semantically, is it correct to say that typeof "returns" something ?

Mathematically, an operator is a function, but one that uses special
syntax. It is correct to say "returns"; that's the word the Standard
uses. Also an operator isn't implemented by a Function object, or at
least not by one that's visible to users.

John

Thomas 'PointedEars' Lahn

unread,
Sep 25, 2016, 6:54:05 AM9/25/16
to
mireero wrote:
^^^^^^^
Your real name belongs in your From header field value if you desire
more/further answers.

> Is there any good reason for using "===" instead of "==" (apart from a
> slight improve in performance, I once read a 4% faster for "===") ?

Yes.

> If I have:
> typeof some_var;
> This will always returns(*) a string like in:
> console.log(typeof document);
> => "object"
>
> If I do the comparison of typeof some_var against a string, there
> shouldn't be any casting.

Correct.

> When someone wants to test if some variable is defined, I usually see:
> if (typeof some_var === "undefined") {
> Would I have the expected and identical behavior with:
> if (typeof some_var == "undefined") {

Yes, except that your code linter might give you a confusing warning.

> I can't see why it wouldn't but as I keep reading "===", I thought I'd
> better ask!

AFAIK, the code linter that considers the type of expressions when checking
the use of equals operators is yet to be written.

> (*)
> typeof is an operator, not a function.

Correct. Therefore you have correctly _not_ written it like a function call
even though the grammar would allow that.

> Semantically, is it correct to say that typeof "returns" something ?

I do not think so, although the issue becomes more complicated with operator
overloading (that, AFAIK, ECMAScript [ES] implementations so far only
support implicitly with the .toString() and .valueOf() methods). Therefore,
in order to avoid misconceptions, when talking about operators in ES
implementations, I use the verbs “(to) yield” and “(to) result (in)”
instead.

--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

mireero

unread,
Sep 25, 2016, 9:12:52 AM9/25/16
to
Thanks for both answers, all clear now!

(Should I add "resolved" or something in the "Subject"?)

Thomas 'PointedEars' Lahn

unread,
Sep 25, 2016, 9:22:38 AM9/25/16
to
mireero wrote:

> Thanks for both answers, all clear now!

You’re welcome.

> (Should I add "resolved" or something in the "Subject"?)

No, because there no problem was solved.

You should add your real name in the “From”, though.
0 new messages