10

How come I can use += on a string, but I cannot use -= on it?

For example...

var test = "Test";
var arr = "⇔"

test += arr;
alert(test);  // Shows "Test⇔"

test -= arr;
alert(test);  // Shows "NaN"
3
  • 2
    Note: I am no dummy; I know you cannot "subtract" a string. Somebody asked me this and I was not sure how to respond, so I am posting it here. Commented Nov 17, 2009 at 19:38
  • The + operator is the concatenation operator when one of the operands is a string. Commented Nov 17, 2009 at 19:38
  • 1
    Only two operators are defined for strings: + and += (developer.mozilla.org/en/…) Commented Nov 17, 2009 at 19:54

6 Answers 6

13

The short answer is - it isn't defined to work with strings.

Longer answer: if you try the subtraction operator on two strings, it will first cast them to numbers and then perform the arithmetic.

"10" - "2" = 8

If you try something that is non-numeric, you get a NaN related error:

"AA" - "A" = NaN
Sign up to request clarification or add additional context in comments.

Comments

7

Because the + operator concatenates strings, but the - operator only subtracts numbers from each other.

As to the why -- probably because it is difficult to determine what people want to do when they subtract strings from each other.

For example:

"My string is a very string-y string" - "string"

What should this do?

Comments

3

As all said, the -= operator is not overloaded to work with Strings, it only works with Numbers.

If you try to use it with strings, the operator will try to convert both operands to Number, that is why you are getting NaN, because:

isNaN(+"foo"); // true

To get rid of the arr content on your test string, you can replace it:

var test = "Test",
    arr = "⇔"

test += arr;
alert(test);  // Shows "Test⇔"

test = test.replace(arr, ""); // replace the content of 'arr' with "" on 'test'
alert(test);  // Shows "Test"

Comments

1

That's because the minus sign is not a valid String operator, whereas the plus sign is overloaded to handle both Numbers (addition operator) and Strings (concatenation operator).

What results were you hoping to get from this?

2 Comments

Hoping the "decrement" operator would behave like a substring and rip out what was previously appended.
But even with numbers the minus sign is not "decrement", that's minus-minus --. Minus-equals -= is just a convenience operator that's a compound operator analagous to the expression a = a - b
0

Generally, programming languages don't define subtraction for strings. += isn't really addition in the first place, it's concatenation.

1 Comment

php is a lot better for dealing with strings
0

Because the +(plus sign) is also the string concatenation operator, while the -(minus sign) only applies to subtraction. If JavaScript can append 2 strings together it won't complain, but if you try to subtract 2 strings, it just doesn't make any sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.