4 ms·
"JavaScript type coercion is such a mess" - in what way? Seriously.
by benaston 12y ago
"JavaScript type coercion is such a mess" - in what way? Seriously.
- danbruc 12y agoIn what way it isn't? Just look at this beauty [1] and its consequences. But probably nobody uses the equality operator anymore because it is such a mess. Less than is even more messed up [2]. [1] http://people.mozilla.org/~jorendorff/es5.html#sec-11.9.3 http://people.mozilla.org/~jorendorff/es5.html#sec-11.9.3 [2] http://strilanc.com/visualization/2014/03/27/Better-JS-Equality-Table.html http://strilanc.com/visualization/2014/03/27/Better-JS-Equal...
- nfm 12y ago`" " == 0 == false`, and `"" == 0 == false`, but `" " != ""`. `"Infinity" == Infinity`, but `"true" != true`. I could go on for a while like this.
- coldtea 12y agoAnd which of those persist if you use ===?
- tragic 12y agoNone - because === does not coerce types. Type coercion, not double-equal weirdness as such, is the gripe of the GGP. Non-transitive, inconsistent equality is nothing more than a symptom of JS's rules for implicit conversions. Language-war disclaimer: I love javascript and everything, it's a very expressive language; but a good wodge of the tooling around it nowadays is to help people avoid things like implicit type-coercion 'surprises'.
- benaston 12y agoThat is not specifically type coercion, but the behavior of the equality (==) operator in JavaScript. Type coercion in JavaScript can be very useful, for example !!('foo') is coercive and easily understood (and has an unsurprising result). Thankfully the == operator is completely optional and has a more easily understood === (identity) correspondent, making your point less about a language and more about a specific operator within a language.
- nfm 12y agoIt is type coercion. The runtime converts both operands to the same type before making the comparison (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...). This is also true of !, <, and friends. Other operators in JavaScript may behave more intuitively than ==, but I don't think you can really make a good case for JavaScript's type coercion being 'unsurprising'.
- benaston 12y agoI suppose what I meant was that you point out that the coercive behavior of some operators is counter intuitive; ergo type coercion in JS is a mess. I dont think that follows because some operators behave in a coercive fashion more convenient than many other languages.
- protester 12y agoWell, your first line doesn't make sense, since the first two comparisons are using type coercion but the third isn't, so it's like saying `0 == false` but `0 !== false` how can this be? (" " != "" is the same as " " !== "") The second line at least uses type coercion, but still you are making the wrong assumptions. true could be coerced to many strings 't', '1', 'true', 'yes', 'on', but they chose to use '1' (You may not like it but I think it's a good choice). Infinity on the other hand has not many choices when coercing it to a string I can think of '∞' (which is difficult to type), 'Infinity', and maybe 'Inf.' so I think they made a good choice here. I'm not saying type coercion in js has no problems, but you said that it's a mess and I just think you chose the wrong examples.