Why Does JavaScript Handle the Plus and Minus Operators Between Strings and Numbers Differently

Why does JavaScript handle the plus and minus operators between strings and numbers differently?

String concatenation is done with + so Javascript will convert the first numeric 1 to a string and concatenate "1" and "1" making "11".

You cannot perform subtraction on strings, so Javascript converts the second "1" to a number and subtracts 1 from 1, resulting in zero.

Why does Javascript evaluate plus and minus differently when operating on int and string

Because the plus sign implicitly converts 1 to a string

And the minus sign implicitly converts "1" to an int.

Per Request

tl;dr; The ECMA 5 spec states that if the left or right side of the operator is a string then return the string result of concatentating. Where as the minus operator just operates on numbers so it will convert both sides to numbers

http://es5.github.io/#x11.9.3

11.6.1 The Addition operator ( + ) # Ⓣ Ⓡ Ⓖ
The addition operator either performs string concatenation or numeric addition.

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 lprim be ToPrimitive(lval).

Let rprim be ToPrimitive(rval).

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

Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.

NOTE 1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All native ECMAScript objects except Date objects handle the absence of a hint as if the hint Number were given; Date objects handle the absence of a hint as if the hint String were given. Host objects may handle the absence of a hint in some other manner.

NOTE 2 Step 7 differs from step 3 of the comparison algorithm for the relational operators (11.8.5), by using the logical-or operation instead of the logical-and operation.

11.6.2 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. See the note below 11.6.3.

Why does the + operator consider the number as string when added

+ will only add two numbers if it has a number on the left hand side and a number on the right hand side.

'3' + 4 + 5;

First '3' + 4 has a string on the left hand side. So it converts the right hand side to a string and concatenates them.

Second '34' + 5 has a string on the left hand side. So it converts the right hand side to a string and concatenates them.

3 + 4 + '5';

First 3 + 4 has a number on both sides, so it adds them. Second 7 + '5' has a string on the right hand side, so it converts the left hand side to a string and concatenates them.

How does adding String with Integer work in JavaScript?

"" + 1          === "1"
"1" + 10 === "110"
"110" + 2 === "1102"
"1102" - 5 === 1097
1097 + "8" === "10978"

In JavaScript, the + operator is used for both numeric addition and string concatenation. When you "add" a number to a string the interpreter converts your number to a string and concatenates both together.

When you use the - operator, however, the string is converted back into a number so that numeric subtraction may occur.

When you then "add" a string "8", string concatenation occurs again. The number 1097 is converted into the string "1097", and then joined with "8".

JavaScript: Why would 1 - 1 return 0?

The subtraction operator (-) subtracts the number to the right of the
operator from the number on the left.

When either of the operands are strings, an attempt is made to convert
the strings to numbers.

Source

Why is 1 + '1' = '11' and 1 - '1' = 0 in JavaScript (Coercion)?

- is defined in terms of ToNumber, whereas + has an extra clause for strings (emphasis mine):

11.6.1 The Addition operator ( + )


The addition operator either performs string concatenation or numeric
addition.

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 lprim be ToPrimitive(lval).
  6. Let rprim be ToPrimitive(rval).
  7. If Type(lprim) is String or Type(rprim) is String, then
    • Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)

[...]

Why is string number addition not coalescing intuitively

+ and - evaluate left-to-right. When either operand is a string, the result is concatenation. When both are numbers, the result is addition.

In contrast, - will always coerce both sides to numbers.

'2' + '2' - '2'

does

// left-to-right
('2' + '2') - '2'
// both sides are strings, so concatenate
'22' - '2'
// operator is -, so coerce both sides to numbers
22 - 2
20


Related Topics



Leave a reply



Submit