News for hackers. Stuff that matters.
Re:JavaScript Equality Minesweeper by ttty on Monday September 17, 2018 @05:02AM (#18004242)
It's so funny. Anybody still using == with js?
Re:JavaScript Equality Minesweeper by DougBTX on Monday September 17, 2018 @05:37AM (#18004382)
I'm interested to know what people think about this with TypeScript - quite a lot of the weird behaviour in plain JS is rejected by the compiler, eg, "true == 1" evaluates to true in plain JS, but is a compiler error in TypeScript since it rejects bool vs number equality checks.

So with TypeScript code, I've been continuing to use == as in most other languages, with the expectation that any odd comparisons will be flagged by the compiler. On the other hand, I've not verified myself that it catches them all, so I wonder if anyone has come across other edge cases with this that could cause problems down the line.

Re:JavaScript Equality Minesweeper by slikts on Monday September 17, 2018 @05:42AM (#18004406)
The issue there is that TS allows dropping out of the type system with `any`, so it might not actually be checking the types you're comparing, so I'd still use ===.
Re:JavaScript Equality Minesweeper by kbp on Monday September 17, 2018 @07:09AM (#18004789)
I prefer it when Typescript tries to provide static type checking for Javascript rather than providing checking for how they wish Javascript was. `(x: number, y: string) => x == y` is an error because 'this condition will always return false'. In what language? There are linter rules for enforcing === over ==; Typescript having incorrect type definitions to try and make sure you only use == like === seems like a really poor solution to that.
Re:JavaScript Equality Minesweeper by Sacho on Monday September 17, 2018 @07:28AM (#18004864)
Aren't you implicitly assuming that TS's number === JS's Number and TS's string === JS's String? You could say == is extended for TS's types(number/string), but the JS == still exists: x as any == y as any;
Re:JavaScript Equality Minesweeper by kbp on Monday September 17, 2018 @10:46AM (#18006360)
I can see what you're saying, but I think that argument would be a lot stronger if == compiled to ===. If you want an operator with ==='s semantics, it's there; providing == and claiming it behaves like === just seems like an opportunity for annoying surprises if something slips through the cracks. Since == doesn't compile to ===, I wouldn't use it unless I understood its semantics and wanted them.
Re:JavaScript Equality Minesweeper by arisb on Monday September 17, 2018 @08:32AM (#18005275)
I'm guessing you can't use `(x: number, y: string) => x === parseInt(y)`?
Re:JavaScript Equality Minesweeper by jaaames on Monday September 17, 2018 @09:19AM (#18005639)
As long as you feed parseInt it's second radix parameter you can :p
Re:JavaScript Equality Minesweeper by simias on Monday September 17, 2018 @05:19AM (#18004303)
Plenty of beginners I'm sure. People who come from languages where == is not broken will probably default to using it, if only because of muscle memory. It might not be immediately obvious that it's broken too which makes it worse.
Re:JavaScript Equality Minesweeper by atirip on Monday September 17, 2018 @07:47AM (#18004949)
Yes. In our codebase it is forbidden to test for true/false, only truthy/falsy is allowed. The only place where === is needed and allowed is testing for if function argument is omitted. Otherwise == and yes, all developers understand that and agree and no, we haven’t had any bugs because of that. Big and complex web app, not my first, the previous gig was even bigger, same approach. Works like a charm.
Re:JavaScript Equality Minesweeper by noxToken on Monday September 17, 2018 @08:31AM (#18005260)
But why? I'm having a hard time understanding why you'd outright ban identity (===) and only allow equality (==).

I can think of one instance where I needed identity instead of equality, and I took the time to refactor to ensure that equality would work.

Re:JavaScript Equality Minesweeper by atirip on Monday September 17, 2018 @08:45AM (#18005376)
We fully embrace Javascript as dynamically typed language and when one does this, types on most cases will become irrelevant. Then also using === in the code means that the usage is explicitly required in this specific place and == will cause wrong result. But when === and == are the same, then we use strictly ==. Also read this: http://dmitry.baranovskiy.com/post/typeof-and-friends

If you are curious, please post some real life real working code where in your mind usage of === is absolutely needed and I try my best to explain how I would tackle that with ==.

Re:JavaScript Equality Minesweeper by sdegutis on Monday September 17, 2018 @10:01AM (#18005980)
> "We fully embrace Javascript as dynamically typed language and when one does this, types on most cases will become irrelevant."

That's not dynamic typing, that's weak typing.

Dynamic typing just means the types are omitted in the source code, but they're still there. typeof 3 is still 'number'. If you explicitly specify the types, you get TypeScript.

But weak typing means you can do things with disparate types that you can't normally do, usually by implicitly coercing one to the same type as the other.

The world has generally considered weak typing a very bad idea. Type coercion has too many edge cases and unintuitive/unexpected combinations. Even Lua is experimenting with runtime flags that disable type coercion such as "1" + 2. (That's the reason they have the .. operator btw.)

The only byproduct of weak typing that most people agree on keeping in modern languages is the concept of "truthy", but even then, languages have different ideas of what should be truthy. Clojure and Ruby say everything except nil and false are truthy, Python says "" and [] are also falsey, and I don't remember where JavaScript fully stands on this, but I know that null, undefined, false, and "" are at least falsey.

Re:JavaScript Equality Minesweeper by atirip on Monday September 17, 2018 @10:24AM (#18006182)
Yep, you are correct about generic truthy. We have standard library function empty(), which is custom (agreed internally what is empty) and which everyone knows. That solves many issues you pointed.
Re:JavaScript Equality Minesweeper by kbenson on Monday September 17, 2018 @10:41AM (#18006328)
> The world has generally considered weak typing a very bad idea.

Now that is the type of thing that screams for supporting evidence.

> Type coercion has too many edge cases and unintuitive/unexpected combinations.

That depends on the implementation. I would say Perl handles this problem neatly by making the coercion done entirely based on the operator used, so it's always obvious.

Re:JavaScript Equality Minesweeper by sdegutis on Monday September 17, 2018 @10:49AM (#18006394)
> Now that is the type of thing that screams for supporting evidence.

I think the direction of mainstream programming languages over the past 20 years is overwhelmingly clear about this.

There's only one language that I know of that's still in popular use and that likes weak typing.

> That depends on the implementation. I would say Perl handles

Yep that's the one.

Re:JavaScript Equality Minesweeper by kbenson on Monday September 17, 2018 @11:14AM (#18006695)
> There's only one language that I know of that's still in popular use and that likes weak typing.

As much as I wish it wasn't so, I believe PHP sees more activity currently than Perl, and it's also weakly typed.

Javascript itself is weakly typed as well, which is why we're all having this discussion.

I would say that two of the most popular languages (probably the two most popular new languages) of the last two decades have been weakly typed.

Re:JavaScript Equality Minesweeper by sdegutis on Monday September 17, 2018 @11:18AM (#18006739)
That's a fair point, but I think this article shows that weak typing in JS is actively avoided by most people. That's why the article is interesting in the first place. I don't know if PHP heavily relies on weak typing though, I haven't used it in 9-10 years. But JS doesn't rely on it. It's just there, and easily avoided.
Re:JavaScript Equality Minesweeper by snek on Monday September 17, 2018 @09:23AM (#18005688)
> please post some real life real working code where in your mind usage of === is absolutely needed and I try my best to explain how I would tackle that with ==.

I think you're expending your energy for the wrong reasons. There is no need to be "clever" when writing code. Being explicit is always better. Being explicit is how bugs are avoided, how new people can enter your codebase, and how you can maintain your codebase in the future. If you spend time thinking about how to justify usage of something in your code, it has no justification being used.

Re:JavaScript Equality Minesweeper by atirip on Monday September 17, 2018 @09:31AM (#18005741)
Disagree. Nothing clever in this, just elementary JS knowledge. I claim that relying in types in dymanic language (always ===) is actually worse. Better is not rely on types.
Re:JavaScript Equality Minesweeper by sdegutis on Monday September 17, 2018 @10:11AM (#18006061)
The types are there, are to some extent you always have to rely on them. You can't call .push() on a string, or pass a number to Object.keys(). I can't imagine a situation where using == even works as you expect it to, aside from comparing numbers and strings that contain numbers. In which case the string should be converted to a number at the user input edge, as part of (but not the entire) input normalization, sanitation and validation phase.
Re:JavaScript Equality Minesweeper by snek on Monday September 17, 2018 @09:40AM (#18005809)
> Nothing clever in this, just elementary JS knowledge.

How's your score on the minesweeper? :)

Re:JavaScript Equality Minesweeper by atirip on Monday September 17, 2018 @10:03AM (#18005997)
Probably bad ;-) Seriously speaking all that comparison tables are pure academic stuff. In real code I honestly ever encountered a case where I have no clue whatsoever what both sides of equation are. To the claim that when you have such conditions, then you need to fix that and such code is bad to the bones. Real life code is much more boring - if (a == “b”) where there is no value possible for a where === would give different result. But, I like to alleviate that there is no beef for me in this discussion, i just wanted to show that, yes, someone still uses == and he is not looney ;-)
Re:JavaScript Equality Minesweeper by simias on Monday September 17, 2018 @09:01AM (#18005489)
>We fully embrace Javascript as dynamically typed language [...]

I won't outright dismiss your coding style because I don't know what your codebase looks like and maybe you're genuinely talented JS devs who know what they're doing (something I'm decidedly not) but your comment makes it sound like discouraging the use of '==' is not "embracing" JS's dynamism. I don't think I agree with that, actually there are a bunch of highly dynamic languages where comparison is not such a mess. The main problem with '==' is that it's highly inconsistent and intransitive (as showcased by TFA). It's tricky to figure out what evaluates to true or false when types don't match. It's not PHP bad but it's pretty damn bad.

Re:JavaScript Equality Minesweeper by noxToken on Monday September 17, 2018 @11:50AM (#18007034)
I'm certain that at least 95% of our code would be safe if you replaced === with ==. We likely have some edge cases that would blow up, but by and large, we don't really depend on the type checking of identity. We write code as if you explicitly declare type on JS.

I don't think using equality is inherently bad. I think that it can lead to a lot of bad habits for junior devs who don't understand some basics about types. I'm not against weak/implicit typing systems, but my experience has taught me that junior devs (and student alike) are prone to errors extending from the type system.

Re:JavaScript Equality Minesweeper by atirip on Monday September 17, 2018 @01:33PM (#18007997)
Yes. To combat that we have code reviews (pull request workflow) and generally friendly and supportive atmosphere.
Re:JavaScript Equality Minesweeper by sonnyblarney on Monday September 17, 2018 @03:33PM (#18009204)
"We fully embrace Javascript as dynamically typed language"

Ok, but why? Do you not think there's an advantage in having a difference between strings and numbers?

"1" == 1?

You can chose that if you want, surely, but what are the advantages of 'embracing' this?

Re:JavaScript Equality Minesweeper by z3t4 on Tuesday September 18, 2018 @05:04AM (#18013635)
If you do arithmetic's it's converted to Number, if you concatenate it's converted to string. It's however unfortunate that + is used for both concatenation and addition. The advantage is flexibility and convenience. You can for example have functions that takes almost anything as input and does what you want, so that you do not have to tell the computer in detail what types to use.

For example in Node.JS callback convention the last argument is always the callback functions, even if the function takes 4 arguments, you can put the callback in the 3:rd argument and it will figure out which one is the callback function. Then the first parameter in the callback function is either null or an Error.

Another example is RegeExp where you can write if( str.match(/foo/) ) instead of if ( str.match(foo) === null )

It's also convenient that empty strings are "falsy" which allow you to write if(!foo) instead of if( foo==="" || foo===undefined || foo===0 )

Re:JavaScript Equality Minesweeper by sonnyblarney on Tuesday September 18, 2018 @06:08PM (#18019636)
Ok, but I really don't think that any of those conveniences outweigh the downsides of total lack of typing.

Also - you can use the == 'falsiness' whilst still in a strict scenario.

If you take the typing argument one step further, into Typescript, the advantages of just some basic typing become considerably more clear as the compiler/transpiler picks up loads of problems that wouldn't be found otherwise.

Re:JavaScript Equality Minesweeper by z3t4 on Wednesday September 19, 2018 @04:20AM (#18022692)
The only problem I can see is mixing strings and numbers. But there are probably more errors someone pro types would say is type errors while someone pro dynamic/loose typing would say is not ... Here's a nasty bug that took me like two hours to debug (this is only two lines, but imagine a sea of code where such thing could easily hide). Can you spot it ?

    var foo = [];
    foo.push["bar"];
You could say it's a type error. It's however not caught by neither TypeScript or Flow.
Re:JavaScript Equality Minesweeper by sonnyblarney on Wednesday September 19, 2018 @04:40AM (#18022781)
If you want to type an array, you can do that in Typescript.

let x:number[] = []; x.push("x");

Here, the transpiler will catch the error of trying to push a string to a number array.

Typing is your friend, and it will in almost all cases help you write cleaner and safer code.

Re:JavaScript Equality Minesweeper by skrebbel on Monday September 17, 2018 @05:52AM (#18004464)
Everybody who uses >= and <= implicitly uses ==.

That said, if you mix types between comparison operands or inside arrays then you probably have bigger problems than "==" semantics. :-)

Re:JavaScript Equality Minesweeper by slikts on Monday September 17, 2018 @05:38AM (#18004389)
It's idiomatic to use == to check for both null and undefined at least.

A lot of people seem to have a perception of languages as being handed down by some language gods, and apparent problems with the language get explained away by "mere users" lacking knowledge, perspective, discipline, etc.

Re:JavaScript Equality Minesweeper by 11235813213455 on Monday September 17, 2018 @07:39AM (#18004911)
For typeof obj == '..' you don't need === since typeof always returns a string

And the engine makes it as fast as === https://github.com/dperini/nwsapi/pull/11#issuecomment-38327... (Generally else == is slower than ===)

Re:JavaScript Equality Minesweeper by marijn on Monday September 17, 2018 @09:12AM (#18005572)
As long as the types on both sides match, the behavior of == is perfectly reasonable, and in typical uses, this will be the case.
Re:JavaScript Equality Minesweeper by mishoo on Monday September 17, 2018 @09:23AM (#18005685)
I do, when I know the types match. Also, I always use == to test if something is either null or undefined; cases where the distinction matters are extremely rare, and this looks too silly: typeof foo === "undefined" || foo === null.
Re:JavaScript Equality Minesweeper by paradite on Monday September 17, 2018 @07:17AM (#18004821)
People who writes `if(value)`
Re:JavaScript Equality Minesweeper by blauditore on Monday September 17, 2018 @07:41AM (#18004914)
But that's not the same; it's about truthy-ness. E.g. -1 and 1 are both truthy, but not loosely equal.
Re:JavaScript Equality Minesweeper by paulintrognon on Monday September 17, 2018 @08:04AM (#18005044)
That's weird, I would thought that if(value) was the equivalent of if(value == true). Learned something today!
Re:JavaScript Equality Minesweeper by pythonaut_16 on Monday September 17, 2018 @03:29PM (#18009168)
You can force cast a boolean by doing if(!!value == true) {}, but at that point you might as well just say if(value)
Re:JavaScript Equality Minesweeper by paradite on Monday September 17, 2018 @07:45AM (#18004940)
You are right. I confused truthy-ness with '== true' for some reason, didn't write much JS lately.
Re:JavaScript Equality Minesweeper by christophilus on Monday September 17, 2018 @07:58AM (#18005010)
We use it to check if a value is null or undefined (`foo == null`) is true for both `foo = null` and `foo = undefined`, which is pretty handy. Other than that, we don't use `==`.
Re:JavaScript Equality Minesweeper by yoklov on Monday September 17, 2018 @11:13PM (#18012192)
Firefox has a few million lines of JS code and the coding standard prefers `==` to `===`. I don't think it's linted for though.
Re:JavaScript Equality Minesweeper by CraneWorm on Monday September 17, 2018 @05:29AM (#18004342)
People relying on its broken behaviour :)
Re:JavaScript Equality Minesweeper by darepublic on Monday September 17, 2018 @07:21AM (#18004837)
Truthy ? A : B Truthy && A Falsy || fallback