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


Groups > comp.lang.javascript > #31446

Re: typeof and strict equality

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: typeof and strict equality
Date 2016-09-25 12:54 +0200
Organization PointedEars Software (PES)
Message-ID <6589965.EvYhyI6sBW@PointedEars.de> (permalink)
References <57e795fa$0$7983$426a74cc@news.free.fr>

Show all headers | 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