Note 2. ECMAScript. Equality operators.

  • Thread starter Dmitry A. Soshnikov
  • Start date
D

Dmitry A. Soshnikov

I'll see, maybe I'll mention some cases explicitly, sort of: "this note
isn't an appeal for everyone to use == when it can be used and bother
yourself and remember much all the ambiguous cases, but to show that
there are completely safe cases when usage of === is strange and that
you should not afraid of == operator". I don't know how exactly to form
this sentence, but I'll add it.

I've updated:

"And this note isn't an appeal for everyone to use == when it's can be
used and to bother yourself with remembering all the ambiguous cases
related to implicit type conversion, of course not. However, here I'd
like to point out that there are many cases when strict equal === is
non-necessary and when it doesn't bring any additional robustness to the
code. These cases are <em>completely safe</em>, related to the
<em>standard behavior and algorithms</em> of the ECMA-262 specification
and usage of === with them can look a bit strange. Let's see it on the
example."

So, is it more "politically correct" now?

Dmitry.
 
D

Dmitry A. Soshnikov

Already discovered by google as good practice. See presentation about
changes at the language.
<URL:
>

Yes, I saw this lecture before of course. Generally, the lecture is good
(as an overview), M.Miller just explains the ability but not force to do
this.

But the another topic -- to "freeze & strict everything" - yeah, very
debatable.
| Changes to JavaScript, Part 1: EcmaScript 5

That title is broken in first place. The word "change" in such a
context is association with breaking backward compatibility.
"Improvements" is significant better than "Changes".

The interesting parts are after 23rd minute. The are trying to fix the
follow "problem":

function Point(x, y) {
this.x = +x;
this.y = +y;
}

var pt = new Point(6, 3);
/*Clobbers pt's consistency*/
pt.x = "foo";

So, who would try to do this? If someone wants to shot him self, he is
free to do this. That is not our problem, also is not a problem of
ECMA-262 and is not a problem of google.

Yes, I think this is again explained from the position of a user which
plays in his sandbox (a web-page) and change everything and all via
console or "javascript:" pseudo-protocol. Will a programmer do this? To
break his code -- very debatable.
This cannot be done. They would break backward compatible and such a
decision would break working of many scripts. There are too many
scripts which augment built-in objects. Also, such a decision would be
in contradiction with their decision about `Object.' API.

Yes, of course. As I remember, Brendan Eich don't wanna this (freezing
built-ins) too.

As I told (and my position still the same): misconception is not to
augment built-ins (in a local project with avoiding consequences), but
misconception is to have mutable non-frozen built-ins and call as a
misconception their augmentation. I told -- if this is a misconception
-- just do it more radically, as Python did -- just "freeze them all"
that programmers have no temptation to do this. On what the reply was --
<https://mail.mozilla.org/pipermail/es-discuss/2010-April/010973.html>.
So, of course it won't be done, at least soon, by several reasons.

Dmitry.
 
N

nick

when you type in the code
`typeof` and comparing it with a string -- why do you use ===?
Objectively. For consistency? Or what?

I wrote it like that in some code I posted here one time, not
realizing it was a big deal. The reason I wrote it that way was
because I know typeof returns a string primitive, so I felt the code
was better (self-)documented by writing ===, because it's like saying
"that expression on the left should always give a string primitive" in
case the reader 'forgot' or whatever. I guess the fact that you're
comparing it to a string on the right is probably enough, I just felt
(at the time) like === made the whole statement more explicit. The
whole thing sounds like a stylistic issue to me; the functionality of
the two operators in the case of typeof is basically identical as
discussed here ad nauseum.
 
D

Dmitry A. Soshnikov

I wrote it like that in some code I posted here one time, not
realizing it was a big deal. The reason I wrote it that way was
because I know typeof returns a string primitive, so I felt the code
was better (self-)documented by writing ===, because it's like saying
"that expression on the left should always give a string primitive" in
case the reader 'forgot' or whatever. I guess the fact that you're
comparing it to a string on the right is probably enough, I just felt
(at the time) like === made the whole statement more explicit.

This is an interesting reason, I should admit. Sort of, vice-versa --
you /know/ how standard operators or function work (values of which
types they return), but use === anyway just to make it more explicit
(possibly, by the ideology -- "explicit is better than implicit").
The
whole thing sounds like a stylistic issue to me;

Possibly. And maybe even yes. What I thought. The note as its now --
really looks like a bit emotional but not analytic write-up. I don't
like this stylistics, it isn't mine (if to consider all other chapters),
it's not my reputation.

Although, I wanted to show that == and === are completely equivalent and
there is no any magic in ===, I should admit that the current style
sounds like I'm forcing my readers to use == when it can be used. Even
the notes I've made (that is not so, and the note is just to show
specific safe-cases) are not enough.

Still it can sound like Jorge has heard it -- like a nit-picking to
non-essential thing. And that is worse, just like I'm calling as
"idiots" those who use === with `typeof`. I want to assure that /it's
absolutely not so/, the thing was to point the technical detail, that
they are equal and there are safe cases.

But I've made one mistake -- the note is small if to compare with other
my articles. But at the same time, the note is big for the reason I
wanted to write it -- I should did it in two sentences, but then the
article would have no sense. Objectively, it may have no sense even now.
But I hope, who have understood it correctly, the sense is. Anyway, it
wasn't vain that I mentioned it -- because some programmers change their
mind in this question.

But if the answer for the question is:

"I don't even think and don't care about whether these algorithms are
equal or not, I just _use_ === as it would be the only one equality
operator in the language. Yes, I like to write all that explicitly,
because it helps me not to confuse in the code. Like in Python. You said
yourself that all languages should be so -- not to confuse a programmer
with thinking on some irrelevant with the major task thing -- a ==
operator. I just avoid such situations."

And this is correct. But I mentioned the same.

So, what have I decided? I'll a bit rewrite this article, removing all
the emotional stuff such as ("If the cost is the same, for what to buy
this `stuff`?"). But, of course I won't refuse from the article, still
the technical part that there are cases as `typeof` and that algorithms
are equal is at least -- "a good to know".

Once again, it's _not_ a nit-picking (and this thing I didn't want the
most), but unfortunately it can sound like it (and Jorge is right from
this position). Because, if so, there can be case when someone will
catch me on using `[1, 2, 3].indeOf(1) === 0` and I should explain, why
did I use it after such article. And even if I said about my rules --
that I can use both == and === whenever I want, it will sound odd.

I describe some pit-fall cases (including "falsy" values and boolean
cases), but still mention that == is the same as === in some safe-cases.
At the same time I won't recommend to avoid something (as Crockford
did). Sort of as VK suggested.
the functionality of
the two operators in the case of typeof is basically identical as
discussed here ad nauseum.

Yes, and this is what I wanted to say, identically of the == and === in
such case.

Dmitry.
 
D

Dmitry A. Soshnikov

On 29.06.2010 12:18, Dmitry A. Soshnikov wrote:

What I thought. The note as its now --
really looks like a bit emotional but not analytic write-up. I don't
like this stylistics, it isn't mine (if to consider all other chapters),
it's not my reputation.

So, what have I decided? I'll a bit rewrite this article, removing all
the emotional stuff such as ("If the cost is the same, for what to buy
this `stuff`?"). But, of course I won't refuse from the article, still
the technical part that there are cases as `typeof` and that algorithms
are equal is at least -- "a good to know".

I describe some pit-fall cases (including "falsy" values and boolean
cases), but still mention that == is the same as === in some safe-cases.
At the same time I won't recommend to avoid something (as Crockford
did). Sort of as VK suggested.

Done. Now I like it more. I should admit that the previous version was
the worst my article -- objectively. Because it was too emotional, but
not analytic.

After corrections, it's much better:
<http://dmitrysoshnikov.com/ecmascript/note-2-ecmascript-equality-operators/>

Thanks all, especially VK, Jorge and nick.

Dmitry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top