And now you may want to understand why it has seemingly meaningless rules where
NaN === NaN; // is false
 == !; // is true
Seems like we are trying to write out “bacteria” to the console by concatenating strings together. But in fact, this is what we get back:
// This will return "12" as 1 will be converted into a string 1 + "2" typeof (1 + "2") // Will return "string" // This will return 12 as "12" will be converted into a number "12" * 1 typeof ("12" * 1) // Will return "number"
Understanding Why Banana Happens
In the middle of the string, we try to convert the letters into numbers with the plus sign. This results in “cteri” converted to NaN — Not A Number.
If you leave out the call to
toLowerCase, you can clearly see that we have “baNaNa”.
Then the separate strings connected together into one with the other plus signs and everything is turned to lowercase to hide the culprit. You could have everything in place of “cteri”. As long as it cannot be converted into a number, you’ll get bananas back.
// returns "banana" ('b' + 'a' + + 'cteri' + 'a').toLowerCase(); // returns "banana" ('b' + 'a' + + 'llerin' + 'a').toLowerCase(); // returns "banana" ('b' + 'a' + + 'a-z' + 'a').toLowerCase(); // returns "ba123a" ('b' + 'a' + + '123' + 'a').toLowerCase();
- Access exclusive interactive lessons
- Unlimited access to hundreds of tutorials
- Remove ads to learn without distractions