Jump to content

Add 1000 to 100 to display 1100


heldenbrau

Recommended Posts

well, the + prefix is a shortcut. You could have alternatively done

 

tbox$count.value = parseInt(change$count)+1000;
or even

 

tbox$count.value = Number(change$count)+1000;
The overall takeaway here is that wherever you got change$count from (e.g. grabbing from some form field, receiving an ajax response, etc.), the value was stored as some other type (probably string). So you were basically doing "100"+1000 which javascript interprets as string concatenation.

 

So there are ways (shown above) to convert the value to another variable type (type casting), in order to ensure javascript does what you expect it to do. It is actually one of those best things to check (you can use the typeof operator to check variable type) and/or convert variable type before performing operations, to ensure expected results.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.