Today i saw an example of JavaScript and i can not understand it.
('b' + 'a' + + 'a' + 'a').toLowerCase()
result: "banana"
I don't understand it, why JavaScript add 2 'n' between each 'a'?
Today i saw an example of JavaScript and i can not understand it.
('b' + 'a' + + 'a' + 'a').toLowerCase()
result: "banana"
I don't understand it, why JavaScript add 2 'n' between each 'a'?
The first ‘b’
and ‘a’
are simply strings being added as ‘ba’
. After the second ‘a’
, you see a double plus sign(+), the first one is for concatenation like the previous plus sign. But the second plus sign is called the unary operator which simply transforms the string into a number, if it isn’t already. Since ‘a’ cannot be converted to a number it is converted to ‘NaN’
. The final ‘a’ is added to this ‘baNaN’
string and the final ‘baNaNa’
string is made. And to finish it up, the toLowerCase function is used and the output ‘banana’
is received.