JavaScript > Assignment Operators

Add and assign in JavaScript

How to add and assign value in JavaScript?

To Add and assign value to a particular variable in JavaScript, we can follow this approach.

        function Addassignvalue() {
            var a = document.getElementById("txtA").value;
            var result = 5;
            result += parseInt(a); 
// This is equal to result = result + parseInt(a); alert(result); } </script> Enter digit : <input type="text" id="txtA" name="txtA" /> <input type="button" name="btnAddassignvalue" value="Add & Assign value" onclick="Addassignvalue()" />

In the above code snippet, we have one textbox. On click of button, we are calling the Addassignvalue function that add and assign the number and show result in the alert.

The parseInt() function is used to parse the string to integer so that + operator treat  the value as number and adds otherwise + operator treats it as string and concatenate it.


 Views: 1062 | Post Order: 19

Write for us