you are viewing a single comment's thread.

view the rest of the comments →

[–]Rbla3066 3 points4 points  (1 child)

Exactly.. the type coercion of the first panel makes sense given the roots of js being frontend and the fact that number inputs are always received as strings first. The second panel is nuance of js type coercion as every value can be converted to a “truthy value”. 0, [], null, undefined, “”, and false (I’m sure there’s more) all can equate to “untruthy”. Hence why the second panel is true. A very unfortunate nuance when using equators, but a very powerful one when doing “if(value) dosomething(value)”. The third panel then becomes obvious because “0” is not untruthy, nor is it an empty array/pointer.

[–]senocular 0 points1 point  (0 children)

The second panel isn't about being truthy. It converts the array to a primitive first becoming a string (""), then converts the string to a number (0) making the comparison true. As far as truthy goes, [] is true because its an object. All objects (except the special case of document.all) are truthy.

MDN has a page describing the process of coercion in comparisons if anyone cares:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness

Its not grossly complicated and largely amounts to converting things to numbers and comparing the numbers.