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


Groups > comp.lang.javascript > #31446

Re: typeof and strict equality

Path csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: typeof and strict equality
Date Sun, 25 Sep 2016 12:54 +0200
Organization PointedEars Software (PES)
Lines 54
Message-ID <6589965.EvYhyI6sBW@PointedEars.de> (permalink)
References <57e795fa$0$7983$426a74cc@news.free.fr>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1474800840 20660 eJwFwQcBACAMAzBLlB1mp3v+JZCYOLyeurna2tatwLKTaLHJmNZSl6uJOEPiECl4Wm68/DS/EZU= (25 Sep 2016 10:54:00 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Sun, 25 Sep 2016 10:54:00 +0000 (UTC)
User-Agent KNode/4.14.2
X-NNTP-Posting-Host eJwNwoERwCAIBLCVoPAvjAMq+49gL4FRuZcTdMwvjmydMtnIJhpeyNS6wdV26nZKSQyX5Cf2ABy5EM8=
X-User-ID eJwNy8kBwCAIBMCWBGSVcpbD/ktI5j9uENTZcGx//jiu1MgSdpk1erlEWl/llV7G6gkTUbL/xiDOYGC5kYMPaqAWCw==
Cancel-Lock sha1:rIYYPmkuEof8OA/UdZSdqK3lw0s=
Xref csiph.com comp.lang.javascript:31446

Show key headers only | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web