Jump to content

Increment/Decrement Operators?


violagirl

Recommended Posts

Well, maybe it's not possible to do, but I was trying to make it so x equals some value, and then what would print out to the screen would be one value less. Now I know I could just write x-1 instead, but I wanted to know WHY this didn't work:

var x = 53;document.write(x--);

It spits out the value 53, instead of what I wanted, 52. Is there a reason this won't work? I got the -- to work when assigning to x, like this:

var x = 53;x--

but not in the first situation. Why is that?Also, just on a quick sidenote, I happened to see <script language="javascript" type="text/javascript"> written somewhere. Is there any particular reason one would use this instead of just the shorter <script type="text/javascript>?

Link to comment
Share on other sites

Try thisdocument.write(--a);ordocument.write(++a); ++a will increment first. so it will work fine..but document.write(a++); will print the result and then increment.
You can't use an operator in front of the number recieving the operation. :)Just set the a-- in another variable.x=53--;document.write(x);:)
Link to comment
Share on other sites

You can't use an operator in front of the number recieving the operation. :)
Sure you can. :) pulpfiction is right.
var x = 5;document.write(x--); // prints "5"alert(x); // alerts "4"

var x = 5;document.write(--x); // prints "4"alert(x); // alerts "4"

Link to comment
Share on other sites

Are you sure about this, cos I tried document.write(--a); and it worked fine..... I have also seen such prefix operators in C or C++...I think we can use.http://www.webdevelopersnotes.com/tutorial..._operators.php3
Wow..thanks for teaching me something. :) I never knew they were unary operators.
Link to comment
Share on other sites

So why does this work

<html><head><script type="text/javascript"><!--function increase(a,b) { return ++a * --b; };//--></script></head><body><script type="text/javascript"><!--document.write(increase(2,5));//--></script></body></html>

(it outputs 12), but

return a++ * b--

doesn't in that in outputs 10?If what you said is true, wouldn't the first one first increment a and then multiply the incremented b? That makes sense, because b would be incremented BEFORE it was multiplied but... why doesn't a++ * b-- output 15? Because the ++ IS before the * sign, wouldn't a be incremented, then multiplied by b, and then b would be incremented, thus resulting in 3 * 5? But it seems like it is multiplying before incrementing ANYTHING, even though that ++ IS before the * sign! Why is that! I'm sort of confused here!

Link to comment
Share on other sites

var a = 2;var b = 5;return ++a * --b;

This returns 12 because the following takes place on the return call:1) a in incremented from 2 to 3.2) b is decremented from 5 to 4.3) a (3) is multiplied by b (4) to get 12.4) return returns 12.

var a = 2;var b = 5;return a++ * b--;

This returns 10 because the following takes place:1) a (2) is multiplied by b (5) to get 10.2) a is incremented from 2 to 3.3) b is decremented from 5 to 4.4) return returns 10.In the first case, the varaible values are incremented/decremented before the operation while in the second case, they are incremented/decremented after the operation.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...