Javascript Math numbers add operation can be performed by using plus (+) sign between two variables. Javascript Math object does not provide any method to perform add operation. Addition symbol (+) a plus sign is used to add two numbers but both variables should have number values otherwise it will concatenate both the values. If one variable is number or integer type and other variable has string type value enclosed in quotes then you can use Javascript parseInt function to convert the string value to number. Javascript parseInt function parses the first occurrence of number in the string passed to it but string should have number at its starting index. If string starts with white space then parseInt function trims the leading white space and returns the number. Thus you can add two different types of variable values by using parseInt to convert string to number and variable having number type value directly.
<script language="javascript" type="text/javascript">
var a =10;
var b ="10";
var c =" 20";
var d = 30;
document.write(a + b);
document.write("<br>");
document.write(a + parseInt(c));
document.write(a + d);
document.write(parseInt(b) + parseInt(c));
Output:
1010 30 40 30
Be the first to rate this post
Tags: javascript, javascript math number add, javascript math add, javascript math, javascript parseint, javascript parsefloat, javascript math addition, javascript math adding, javascript math round, javascript convert string to number
8/23/2008 9:11:33 PM