How come I can use +=
on a string, but I cannot use -=
on it?
For example...
var test = \"Test\";
var arr = \"⇔\"
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"