English 中文(简体)
Javascript String Assignment Operators
原标题:

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"
最佳回答

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
问题回答

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?

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"

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?

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

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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签