Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31444 > unrolled thread

typeof and strict equality

Started bymireero <mireero@free.fr>
First post2016-09-25 11:16 +0200
Last post2016-09-25 15:22 +0200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  typeof and strict equality mireero <mireero@free.fr> - 2016-09-25 11:16 +0200
    Re: typeof and strict equality John Harris <niam@jghnorth.org.uk.invalid> - 2016-09-25 11:40 +0100
    Re: typeof and strict equality Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-09-25 12:54 +0200
    Re: typeof and strict equality mireero <mireero@free.fr> - 2016-09-25 15:12 +0200
      Re: typeof and strict equality Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-09-25 15:22 +0200

#31444 — typeof and strict equality

Frommireero <mireero@free.fr>
Date2016-09-25 11:16 +0200
Subjecttypeof and strict equality
Message-ID<57e795fa$0$7983$426a74cc@news.free.fr>
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 ?

[toc] | [next] | [standalone]


#31445

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2016-09-25 11:40 +0100
Message-ID<3cafubdo7iqjhht93tc7u9ir5hhpbpjo1r@4ax.com>
In reply to#31444
On Sun, 25 Sep 2016 11:16:42 +0200, mireero <mireero@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

[toc] | [prev] | [next] | [standalone]


#31446

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-09-25 12:54 +0200
Message-ID<6589965.EvYhyI6sBW@PointedEars.de>
In reply to#31444
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.

[toc] | [prev] | [next] | [standalone]


#31447

Frommireero <mireero@free.fr>
Date2016-09-25 15:12 +0200
Message-ID<57e7cd4d$0$7960$426a74cc@news.free.fr>
In reply to#31444
On 09/25/2016 11:16 AM, mireero wrote:
> 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 ?

Thanks for both answers, all clear now!

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

[toc] | [prev] | [next] | [standalone]


#31448

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-09-25 15:22 +0200
Message-ID<3065808.iIbC2pHGDl@PointedEars.de>
In reply to#31447
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.

-- 
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web