JavaScript > Arithmetic Operators

Subtraction in JavaScript

How to subtract a number from another in JavaScript?


To subtract two or more numbers in JavaScript, we can follow this approach.

<script>
        function Subtract() {
            var a = document.getElementById("txtA").value;
            var b = document.getElementById("txtB").value;
            var result = parseInt(a) - parseInt(b);
            alert(result);
        }
    </script>
    
    Enter two digit:
    <input type="text" id="txtA" name="txtA" />
    -
    <input type="text" id="txtB" name="txtA" />
    <input type="button" name="btnSubtract" value="Submit" onclick="Subtract()" />

In the above code snippet, we have two textboxes. On click of Submit button, Subtract function is called that retrieves the textboxes values and subtract using “-“ operator. At last shows the result as alert.

OUTPUT

 Views: 9688 | Post Order: 13



Write for us






Hosting Recommendations