Simple Subtraction Tool

Calculate the difference. A straightforward utility for subtracting values, demonstrating input retrieval and arithmetic logic in JavaScript.

-
0

Copy the Script

<script>
function subtract() {
    var a = Number(document.getElementById("val1").value);
    var b = Number(document.getElementById("val2").value);
    document.getElementById("res").innerHTML = a - b;
}
</script>

<input id="val1"> - <input id="val2">
<button onclick="subtract()">Calc</button>
<div id="res"></div>

Frequently Asked Questions

The script will display negative numbers correctly (e.g., 5 - 10 = -5). If you want only positive difference, use `Math.abs(result)`.

Yes. Just remember that JavaScript floating point math can sometimes result in `0.00000001` errors. It is best to multiply by 100, subtract, and divide by 100 for cents.