...

View Full Version : Additions



shlagish
06-02-2003, 02:23 AM
document.write(16/2) will write 8
a=16
b=2
document.write(a/b) will write 8

BUT
document.write(16+2) will write 162
and
a=16
b=2
document.write(a+b) will write 162!!!

AAAAAARRRRRGGGGGGG

What's the deal with that?
How am I supposed to add stuff?

requestcode
06-02-2003, 03:24 AM
That is because JavaScript treats them as strings rather than numbers. In JavaScript to join two strings you us "+". Try this:
document.write(eval(16+2))

liorean
06-02-2003, 12:55 PM
No need to use eval there - just enclose the addition in parentheses:
document.write((a+b));

beetle
06-02-2003, 04:12 PM
Ya, the + operator is what is called 'overloaded' - meaning it's result is dependant on context. Something is causing the parser to assume you want to concatenate two strings. I acutally tested

document.write(16+2);

and I got 18 - so I'm not sure why you get 162.

shlagish
06-02-2003, 09:34 PM
Thanks all, will use extra parentheses ()

Beetle: Either your strange, or I'm strange :)

liorean
06-02-2003, 09:50 PM
Hmm, if the first of them happen to be a string instead of a number, you could use parseInt or parseFloat on it. Or, you could simply write (0+a+b) to assure that they are handles as were they numbers.

(All these problems lie within the automatic type conversion system. 16+'2' should not give 162 but 18, while '16'+2 should give 162; This because in the first case numbers is the "closest" conversion available while in the second, string is)

Owl
06-03-2003, 02:55 AM
Sorry liorean, but you are incorrect on both counts.

(0+a+b) if a is a string, will give you string concatenation
(no matter what b is, string or num).

16+'2' will give the exact same result as '16'+2 both 162.

The algorithm of the addition operator is fairly simple:

If one of the operands (left or right) is a string, perform string concatenation.

If the two operands are numbers, perform numeric addition.

That's it.

I hope you don't treat your patients (assuming you will graduate)
based on the same source of knowledge you used to give those advices.:)

( ) ( )
>>V

liorean
06-03-2003, 03:31 AM
Yeah, I just noticed that. I've been fooling around in three different languages lately, and JavaScript is unlike them all on that operators aren't handled as were they either regular global functions or methods of the first object...

Use parseFloat(string), parseInt(string), Number(string) or 1*string to explicitly get a number from a string, and then perform the addition on that.

chrismiceli
06-03-2003, 03:52 AM
Originally posted by liorean
Use parseFloat(string), parseInt(string), Number(string) or 1*string to explicitly get a number from a string, and then perform the addition on that.

those are the best ways.

shlagish
06-04-2003, 03:26 AM
OWL:

document.write(16+2);

Both operands are numbers, and it still treats it as astring and write 162...

liorean
06-04-2003, 12:11 PM
document.write(16+2) gives 18 on ie6w, moz and op7 at least...

shlagish
06-04-2003, 10:47 PM
does document.write((16+2)) give 18 in all browsers?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum