JavaScript adding a string to a number

后端 未结 8 736
轮回少年
轮回少年 2020-11-29 04:12

I was reading the re-introduction to JavaScript on MDN and in the section Numbers it said that you can convert a string to a number simply by adding a plus operator

8条回答
  •  爱一瞬间的悲伤
    2020-11-29 04:22

    The answer can be found in Ecma262.pdf section 11.6.1:

    If Type(lprim) is String or Type(rprim) is String, then a. Return the String that is the result of concatenating ToString( lprim) followed by ToString(rprim).

    So that will resolve all operations according to precedence, so that as soon the string is found any number, the number is converted to string.

    4 + 3 + "5"
    "75"
    4 + 3 + "5" + 3
    "753"
    

    To read the whole standard, go here.

提交回复
热议问题