77
u/arduinomancer May 03 '19 edited May 03 '19
true == 1 doesn’t seem that weird. Don’t a lot of languages treat non-zero numbers as true?
22
u/delventhalz May 03 '19
Yes. It's a C thing. Doesn't really make much sense when you aren't dealing directly with bytes anymore.
6
u/kescusay May 03 '19
Well, it's kinda weird. Strongly typed languages won't like it. In Java, you can't do this:
System.out.println(true == 1);...because
trueis aBooleanand1is anint. Since the==operator does type checking in Java, it fails and you end up with anincomparable typeserror.12
u/TimoJarv May 03 '19
Well it is prohibited in strongly typed languages, but it is pretty much expected behavior for a weakly typed language, so it's not weird in that context.
10
u/kescusay May 03 '19
True. Which is why people who prefer strong typing find it kinda weird. I mean, this all works:
Boolean(2); //true Boolean("2"); //true Boolean("abc"); //true const myObj = {}; Boolean(myObj); //true Boolean({}); //trueIt's because JavaScript and other weakly typed languages have the concept of truthy and falsy. And in JavaScript, all values are defined as truthy except for
false,0,"",null,undefined, andNaN.Not opposed to it, by the way. I love the fact that I can use syntax like:
if (myObj) { doSomething(myObj); }Much simpler than having to set a Boolean flag everywhere.
2
u/THEtheChad May 04 '19
The only two I find weird are `""` and `NaN`. Even though the string is empty, it still "exists" in my mind. It's a character set devoid of characters, but to me, that's just like an object with no parameters. And, in the case of NaN, because the set literally comprises everything that's not a number. Strings, objects, functions, pointers, symbols, etc. Neither of these should be falsy.
-1
u/TimoJarv May 03 '19
I prefer strong typing myself but it makes sense for 0 to be false and 1 to be true. "", {} and [] on the other hand make no sense. "" == 0 for example. Or the fact that an empty array equals false even though array is an object type and an empty object equals true.
3
u/kescusay May 03 '19
Actually, try
Boolean([]);. Yep, it'strue.But yeah, typing is one of the reasons I've been working on converting all of my JavaScript projects to TypeScript. Still get truthy where I need it, but can instill some discipline in myself along the way.
2
2
u/microbouji May 03 '19
Also, in JS, it's the boolean that's being converted to the number 1, not the non zero number being converted to true. So truly... THERE IS NO SPOON
1
1
May 03 '19
All numbers >0 in JS are true. They're positive values, makes sense to me. Similarly all non-empty strings are true. Empty objects are true but that's kind of different.
Also they "are true" but it's more accurate to say they're truthy. With strict equality checks (===) only true is true as far as I'm aware.
JS' implicit coercion is a useful tool when utilized properly, but like prototype inheritance it's misunderstood because it's atypical from features of classical languages, so it's misconstrued as broken or bad.
18
u/MeowTooMovement4Cats May 03 '19
That’s why we got ===
17
u/ericking97 May 03 '19
Naaah bro, just keep shitting on the language without even trying to learn how to use it
3
3
u/MeowTooMovement4Cats May 04 '19
I thought
===prevents coercion when comparing two different types and solves the problem of what the above gif is depicting, or am I missing something?6
u/ericking97 May 04 '19
You are right, I was referring to assholes who indiscriminately shit on js for problems like above without even spending time learning the syntax.
I personally had many bad experiences with people who refuse to acknowledge that, js is a pretty good language.
2
2
u/jimmyco2008 May 04 '19
I shit on JS for other reasons. I actually like == and prefer == to === because I cant think of an instance where I’d want it to fail when comparing different types. If I’m comparing 1 and 1, great, but if I’m comparing 1 and true, I’ll want it to be true also. I’m aware of what conditionals are checking for, and to me it’s more “unexpected” (for JS anyway) when a conditional evaluates to false when I don’t expect it to, usually because I’m comparing two truthy values of differing types.
I shit on JavaScript because of things like “typeof NaN” returns “number”
1
u/loqtor May 04 '19
This is not a matter of JavaScript. The
floatspec defines that. 😊1
u/HelperBot_ May 04 '19
Desktop link: https://en.wikipedia.org/wiki/NaN#Floating_point
/r/HelperBot_ Downvote to remove. Counter: 255563
11
u/Baryn May 03 '19
I like the truthy/falsy system. When you don't know the rules, it sucks. When you do, it's cool that something which lacks a value (null, undefined, empty string) can be evaluated the same as false in a boolean context.
7
u/mishugashu May 03 '19
true == 1 makes perfect sense if you're not type matching. Boolean is binary. 0 is false, 1 is true. This person is obviously coming from a strict typing language if they think it's weird.
3
2
2
1
u/wiithepiiple May 03 '19
undefined == nullThat's a fun one. Standard style allows == only when testing for undefined/null checks. https://standardjs.com/rules.html
2
u/greymalik May 04 '19
It's convenient to be able to do:
if(foo == undefined)and have it also be true when foo is null instead of having to do:if(foo == undefined || foo == null).
1
u/lo_sicker May 03 '19
Hey! That’s actually pretty nifty, and the concepts you used would actually make this a great project for people trying to practice a lot of the fundamentals of the language. Awesome job, my dude 👍
6
u/AnecD May 03 '19
⚡️ https://js-equality-jackpot.surge.sh/
Repo: https://github.com/mathieu-anderson/js-equality-jackpot