admin管理员组文章数量:1331661
I recently went through this typecast JS question:
Given '11'+10
, the answer is 1110
. It is clear that is not 21
because one is a string and JavaScript will treat both as string and perform concatenation instead of mathematical addition.
Why is '11'-10
is equal to 1
, instead of something else?
I recently went through this typecast JS question:
Given '11'+10
, the answer is 1110
. It is clear that is not 21
because one is a string and JavaScript will treat both as string and perform concatenation instead of mathematical addition.
Why is '11'-10
is equal to 1
, instead of something else?
-
1
because there's no other operation to do with
-
aside from subtraction and every operands in javascript is considered as a number first before doing anything else – Netorica Commented Apr 8, 2014 at 4:42
8 Answers
Reset to default 2Because 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
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.
I am not an expert on JavaScript, but your question presents a great opportunity to learn from and read through the ECMAScript specification. This is version 5.1, so it will do fine for this question. Section 11.6.2 covers the Subtraction operator:
The production AdditiveExpression : AdditiveExpression - MultiplicativeExpression is evaluated as follows:
- Let lref be the result of evaluating AdditiveExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating MultiplicativeExpression.
- Let rval be GetValue(rref).
- Let lnum be ToNumber(lval).
- Let rnum be ToNumber(rval).
- Return the result of applying the subtraction operation to lnum and rnum.
So, for step 1:
- Let lref be the result of evaluating AdditiveExpression.
In your case, that is evaluating '11'. We have lref = '11'.
- Let lval be GetValue(lref).
Per line 1 of GetValue (V), if Type(V) is not Reference, return V. Therefore, we return '11'.
We have lval = '11'.
- Let rref be the result of evaluating MultiplicativeExpression.
In your case, that is 10. We have rref = 10.
- Let rval be GetValue(rref).
Again, applying line 1 of GetValue (V), if Type(V) is not Reference, return V. Therefore, we return 10.
We have rval = 10.
- Let lnum be ToNumber(lval).
lval = '11'. Calling ToNumber('11') will follow the ToNumber specification for Strings, case 1. There are optional whitespaces surrounding the numeric literal itself. Since your example has no whitespace, the result is simply the numeric literal, which is 11.
We have lnum = 11.
- Let rnum be ToNumber(rval).
rval = 10. Calling ToNumber(10) will follow the ToNumber specification for Number. In that case, the input argument itself is returned, and no conversion occurs. That means we return 10.
We have rnum = 10.
- Return the result of applying the subtraction operation to lnum and rnum.
One more time, per the specification:
The - operator performs subtraction when applied to two operands of numeric type, producing the difference of its operands; the left operand is the minuend and the right operand is the subtrahend. Given numeric operands a and b, it is always the case that a–b produces the same result as a +(–b).
We have rnum - lnum = 11 - 10 = 11 + (-10) = 1.
Because of +
is a operator overloading to contact a string and -
operator not basically use with the string.
Using -
string will convert into integer and in +
case string will concate.
In JS +
in also concatenate operator, so if the first variable string then concatenate operator
priority high
-
is only mathematical operator so it provide you subtract result.
+
sign is used for both Number and String concatenate. So it considers as two string concatenate when it found string first. You can use +
prefix or Number
to convert string to number
But -
sign is used for subtraction only applicable to number. So it automatically converts string to number.
Following will solve your issue
(+'11') + 10
(+'11') - 10
(or)
Number('11') + 10
Number('11') - 10
'+' operator is the only airthmatic operator which also used in case for concatinate string. So when a string and integer found it acts as concatination operator. All other airthmatic operator like -, *, / are only for calculation that is why they treat everything as number like:
'20' * 3= 60
'20'/ 2= 10
'20'-5= 15
'20'+ 3 = '203'
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.
本文标签: Javascript addition and subtraction with a string valueStack Overflow
版权声明:本文标题:Javascript addition and subtraction with a string value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742270061a2444135.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论