Godwin Godwin - 2 years ago 83
Javascript Question

Concatenates instead of Addition in Javascript

var tt = gas+0.1
document.write (vartt);


Duplicate

Answer Source

The user entered a string. If you want to do arithmetic with it instead of string concatenation, you must convert to a number. There are many different ways to do that including parseInt(gas, 10), parseFloat(gas), Number(gas) and +gas:

Here's one implementation:

var tt = parseFloat(gas) + 0.1; 
document.write(tt);

Also, your document.write() statement was not correct either. The variable name is just tt, not vartt.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download