The plus operator is defined for numbers and strings and as soon as a string is present on either the left or right side, a string concatenation is perfomed.
If we follow the execution path, this is what happens:
// 1st step
'b' + 'a' -> 'ba'
// 2nd step
'ba' + + 'a' // wait a second!
There are two plus operators in this expression. But one of those is actually a prefix-operator, and not a classical plus.
What it does is converting the right-side argument to a number, but converting
'a'
to a number will actually yield NaN
!Let's continue with following the execution path:
// 1st step
'b' + 'a' -> 'ba'
// 2nd step
'ba' + + 'a' -> 'ba' + NaN -> 'baNaN'
// 3rd step
'baNaN' + 'a' -> 'baNaNa'
// 4th step
'baNaNa'.toLowerCase() -> 'banana'
沒有留言:
張貼留言