Javascript addition and subtraction with a string value

前端 未结 8 1106
猫巷女王i
猫巷女王i 2021-01-20 04:31

I recently went through this typecast JS question:

Given \'11\'+10, the answer is 1110. It is clear that is not 21 because one

8条回答
  •  日久生厌
    2021-01-20 05:16

    Because the subtraction operator is not ambiguous and is only defined for numbers. Hence both operands are converted to numbers first.

    See the specification:

    • Addition
    • Subtraction

提交回复
热议问题