Prevent JavaScript from treating numbers as strings when using '+'

This tip describes a method for preventing JavaScript from treating numbers as strings when using '+'.

Sometimes when using JavaScript for calculations, JS treats numbers as string when I use + (addition). If this happens, you can use - (subtraction) and multiply with -1 (negative 1). This forces JS to treat the number string as a number. Very useful in loops, and when getting the number string from e.g. window.document.all.elemID.innerText


: var a=window.
document.all
var total=a(id1).innerText-
(a(id2).innerText*-1)
alert(total)
This was last published in May 2003

Dig Deeper on JavaScript for Lotus Notes Domino

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

This is a non-optimal solution as it requires more work than is needed. Prefix variables with a + to force JS to treat them as numerics. E.G
var total= +a(id1).innerText
Cancel

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchDataCenter

SearchExchange

SearchContentManagement

Close