admin管理员组

文章数量:1330622

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?

Share Improve this question edited Jan 7, 2015 at 10:34 Quentin 945k132 gold badges1.3k silver badges1.4k bronze badges asked Apr 8, 2014 at 4:37 arunarun 3,6773 gold badges31 silver badges54 bronze badges 1
  • 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
Add a ment  | 

8 Answers 8

Reset to default 2

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

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:

  1. Let lref be the result of evaluating AdditiveExpression.
  2. Let lval be GetValue(lref).
  3. Let rref be the result of evaluating MultiplicativeExpression.
  4. Let rval be GetValue(rref).
  5. Let lnum be ToNumber(lval).
  6. Let rnum be ToNumber(rval).
  7. 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