|
|
|
|
|
by vasergen
1174 days ago
|
|
Not sure what js shell he uses, since it is an old video but in browser or node you will get predictable results for his examples. What you need to know that in js '+' sign will add numbers only if both operands are numbers, otherwise it will coerce both to string type, which is true for {} or []. To convert to string js will use '.toString()' method like this {}.toString() and [].toString(). Now {}.toString() is equal to a string '[object Object]' and [].toString() is equal to an empty string '' (for non empty arrays it will print numbers separated by comma). So if we add anything in JS and on both sides are not numbers we actually add strings! And adding strings means concatenation and it works as you would expect, just put two strings together. Having that, let's take a look on those examples again 1. [] + {} equal to '[object Object]' 2. {} + [] equal to '[object Object]' 3. {} + {} equal to '[object Object][object Object]' So no 'magic' involved there. Note that some results are different as in the video, you can check it in console in your browser UPD: typo |
|
The behavior of your console might be slightly different from the behavior of his console, but there are still many reasonable ways to replicate his results. See my comment below if you're still unsure about this point.