('b' + 'a' + + 'a' + 'a').toLowerCase();
It's worth it calling out that I didn't come up with it. I don't even remember the first time I saw it, but it was interesting to notice the curiosity and confusion it created.
Let's take a deeper look at what happens in each step of that line:
'b'is concatenated with
'a', resulting in
'ba' + + 'a' + 'a'. So far so good.
- Now things get interesting. The next expression is
+ + 'a'. The first
+is a concatenation operation just like the previous one, but then it finds another
+sign, followed by
'NaN', to complete the concatenation operation. Phew!
- Now we have
'baNaN' + 'a', which is the last step of the string concatenation, nothing unusual there.
- Last but not least,
baNaNa.toLowerCase()gets rid of the
NaNto make the joke less obvious.
The key thing happens in step #2. The expression
Of course this is a generalization and there are exceptions, but
1 + '1' === '11' (number + string) and
[1, 2, 3] + 4 === '1,2,34' (array + number) are two good examples of such behavior.
Reality is the language has evolved quite a bit, it has an engaged community, a diligent development process through TC39, and it enables a variety of different solutions in all sorts of platforms and devices. However, that wasn't always the case and some of its (weird) early days behaviors remain the same.
I appreciate its power and all the products that were made possible due to its resilience and extensibility. These peculiarities will remain a good source of jokes for years to come nonetheless, and that is just another thing I love about the language.