JavaScript > Arithmetic Operators

Addition in JavaScript

How to add numbers by entering manually in the number boxes in JavaScript?

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

        function Sum() {
            var a = document.getElementById("txtA").value;
            var b = document.getElementById("txtB").value;
            var result = parseInt(a) + parseInt(b);
    Enter two digit:
    <input type="text" id="txtA" name="txtA" />
    <input type="text" id="txtB" name="txtA" />
    <input type="button" name="btnAdd" value="Add" onclick="Sum()" />

In the above code snippet, two textboxes are there. When the button is clicked Sum function executes that gets the values of both textboxes and add it using “+” operator. At last it shows the result in the alert.


 Views: 941 | Post Order: 12

Write for us