3 ms·
Tangent: to be clear, there is no difference between []+{} and {}+[] in javascript. var a = {} + []; var b = [] + {}; var c = String({}) + String([]) All th
by esrauch 14y ago
Tangent: to be clear, there is no difference between []+{} and {}+[] in javascript.
var a = {} + [];
var b = [] + {};
var c = String({}) + String([])
All three are equal. The order does matter in general because its doing string concatenation which obviously matters ([1] + [2] == "12"), but the fact that js always does string coercion and concatenation anytime you have + without numbers on both side isn't really that crazy.
- ghusbands 14y agoThere is a slight difference between []+{} and {}+[], in fact, but you're unlikely to see it outside of a console. At a console, {}+[] gives you 0, which is different to the result you get above. That's because it's an empty block followed by +[].
- ehsanu1 14y agoType coercion is crazy, if for no reason other than making the behavior of certain code harder to predict. That said, I don't think it's a major stumbling block, and once you learn to avoid relying on it there's no problem. There are plenty of parts of JavaScript like that, but these gotchas are just red herrings. The core language is great.