A problem about js addition

"a" + + "b" // aNaN

Why is the result of this addition "aNaN", has a boss to explain? Thank you very much

Jul.06,2021

+'b' the plus sign here means a plus or minus sign, not an addition operation. A positive or negative sign before a string is converted to the number type.

console.log(typeof '3');   // string
console.log(typeof +'3');  //number
When the

'b' string is converted to number, it will be NaN, and then added (concatenated) to the previous'a 'string, NaN is converted to the string' NaN'

.
'a' + (+'b')

+'b' = NaN


+'b' //NaN
+'1' // 1

forcibly convert the Number type. If the string is numeric, it will be converted to Number,. If not, it will be NaN,. Usually we can do this:

var a = '154544';

+a || +a==0 ? console.log('') : console.log('')

the answers of the bosses are more accurate. Let me add some more information:
the rules of conversion

clipboard.png

clipboard.png

for more detailed content, you need to read the relevant specifications by yourself:
https://tc39.github.io/ecma26.

.
Menu