Javascript Math Number Add

by top54u.com 17 Jul, 2008
Spotlight.....

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.

 

Examples of Javascript Math Number Add

 

<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("<br>");

document.write(a + d);

 

document.write("<br>");

document.write(parseInt(b) + parseInt(c));

 

</script>

 

Output:

1010
30
40
30

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 9:11:33 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....