4 ms·
Why wouldn’t “false” be true? It’s a non-empty string.
by wiseowise 1y ago
Why wouldn’t “false” be true? It’s a non-empty string.
- ascar 1y agoyea that's an odd example to pick. expecting type conversion to add meaning to strings is a programmer problem not a language problem. really comes down to developers not thinking about types and their meaning anymore. there are plenty of javascript examples that are actually weird though, especiall when javascript DOES apply meaning to strings, e.g. when attempting implicit integer parsing.
- 010101010101 1y agoIt’s the _existence_ of an implicit conversion from string to boolean that the parent is pointing out as a problem, not how it’s implemented. But that’s Jãvascript bb
- echoangle 1y agoMaybe I’m missing the example but can you not check the truthiness of strings in basically any high level language? At least python does it the same way and it’s very useful.
- 010101010101 1y agoneither Java nor .Net have implicit string to boolean conversions, the only way to asses the truthiness of a string is an explicit conversion - there’s no “if(someStringVariable)” - that’s just a type error at compile time.
- tialaramex 1y agoFor the same reason dogs aren't the square root of two and justice isn't golf? They're not the same kind of thing at all, if you insist that we should be able to compare them then they're not equal, and since programmers are human and make mistakes probably comparing them is itself a mistake.
- wiseowise 1y agoYou've completely disregarded type system rules of the language, and continue doubling down on your ignorance with ridiculous examples. > They're not the same kind of thing at all, if you insist that we should be able to compare them then they're not equal, and since programmers are human and make mistakes probably comparing them is itself a mistake. It is literally encoded in the spec. https://262.ecma-international.org/13.0/#sec-toboolean https://262.ecma-international.org/13.0/#sec-toboolean
- thfuran 1y agoBut it shouldn’t be
- recursive 1y agoSo what should be the value of `"false" == true`?
- bmicraft 1y agoundefined or TypeError would be suitable.
- recursive 1y agoSo shall we require checking for matching type of first? It doesn't seem like an improvement to me.
- thfuran 1y agoShould a list ever equal seven? Should a float equal a socket? Some comparisons just don't make sense.
- recursive 1y ago> Should a list ever equal seven? Should a float equal a socket? No. > Some comparisons just don't make sense. The comparison makes enough sense to me to determine the result.
- camblomquist 1y agoedit: I'm mostly wrong here. Because "0" is false. In a logical world, a non-empty string being truthy is fine even if the value is "false". Javascript isn't logical.
- wiseowise 1y ago``` > if ("0") console.log("true"); true ``` Excuse me? > In a logical world, a non-empty string being truthy is fine even if the value is "false". Javascript isn't logical. You must hate our illogical world built on C, because it has the same behavior.
- camblomquist 1y agoI did a `"0" == false` which returned true. I may need another cup of coffee before making claims.
- jameshart 1y agoAppealing to C - the land of undefined behavior - as a paragon of logic seems like a strange authority to reach for.
- wiseowise 1y agoThe point is that this is a well-defined (not pun intended) behavior that exists within C, C++, Python, Ruby and probably a handful more popular languages. This set pretty much constitutes like 70%(?) of mainstream languages. Yet only JS gets shit thrown its way in this thread.