Javascript is known to bend the laws of physics, but this one takes the cake.
console.log(('b'+'a'+ +'a' +'a').toLowerCase()); |
Outputs: banana
How is this possible?
'b' + 'a' + + 'a' + 'a' | |
//is evaluated to: | |
'b' + 'a' + (+'a') + 'a' | |
(with precedence rules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) | |
(+'a') attemps to convert to ('a') using (unary plus operator): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#Unary_plus_() | |
Since ('a') is not a number which in javascript write off: NaN <- Not a Number | |
NaN gets coerced to string: 'NaN': | |
----------------------------------- | |
'b' + 'a' + NaN + 'a' => 'baNaNa'; | |
'baNaNa'.toLowerCase(); => 'banana' |
This is bananas right?
If you liked this post you might also like the perks🐱🏍 you will get if you subscribe😁