Javascript addition and subtraction with a string value

前端 未结 8 936
猫巷女王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:22

    Here is the answer to your question:

    It says

    If you have a plus sign between a number and a string, concatenation takes precedence over addition. All other numeric operators, when one operand is a string and the other a number, will attempt to convert the string to a number

    Check String Operators section on the link.

    0 讨论(0)
  • 2021-01-20 05:25

    The addition + is used first for string concatenation (if at least one operand is string) and in other cases as numbers arithmetical addition. The subtraction always converts the operands to numbers. Check this article for more details about addition operator.

    0 讨论(0)
提交回复
热议问题