Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## an easy question

hi guys,

l am trying to learn javascript, although certain things seem to be easy to understand, l still have plenty of questions.

so l was reading some articles on w3schools and l was an interesting table about operations.

http://i1087.photobucket.com/albums/...kesnosense.png

if you check above's table you will see at the increment section as well decrement , you will see that in some cases the x and y dont have the same value, now l was just wondering, what is the difference between x=++y and x=y++ ?

• i don't know the exact order of it but (i pretty sure anyways) one is setting x=y and then adding one to x, the other is setting x=y and then adding one to y

• l am still not understanding the difference between x=++y and x=y++ .

do you actually mean x=++y is adding 1 ? or x=y++ is adding 1 ???

originally it says :

x=++y x=6 y=6
x=y++ x=5 y=6

• so let's look at this statement
Code:
` x=++y`
and let's assume for this example that y=5
what is happening is it is taking y and adding 1
"++y"... now after that is done we set x = to it, thus making "++y" equal to 6 and then setting x equal to 6.
then the other statement
Code:
`x=y++`
now we are taking x and setting it equal to y so now x=5... now we will increment x (y is now out of the equation) so the "++" applies to the x only making it x=6 and y=5

• When in doubt, try it out ...
Code:
```<script type="text/javascript">

var x = 5;  var y = 5;
var str = 'From original: \nx='+x+' and y='+y+'\n';
x = ++y;  alert(str+'x=++y\t'+x+' : '+y);

x = 5;      y = 5;
str = 'From original: \nx='+x+' and y='+y+'\n';
x = y++;  alert(str+'x=y++\t'+x+' : '+y);

x = 5;      y = 5;
str = 'From original: \nx='+x+' and y='+y+'\n';
x = --y;  alert(str+'x=--y\t'+x+' : '+y);

x = 5;      y = 5;
str = 'From original: \nx='+x+' and y='+y+'\n';
x = y--;  alert(str+'x=y--\t'+x+' : '+y);

</script>```

• Alykins wrote:
Code:
`x=y++`
now we are taking x and setting it equal to y so now x=5... now we will increment x (y is now out of the equation) so the "++" applies to the x only making it x=6 and y=5
Not true in JavaScript, and not true in C#, C, C++, Java, PHP, or any other language I know of.

*************

The ++ operator, either pre-increment or post-increment, has the highest prececdence. So that operator will *ALWAYS* take place before the assignment (=) operator.

Post-increment and post-decrement are weird operators. They came about in C because when C was invented computers, in general, didn't have the wealth of registers and operations built in that they have today. Those operators were and attempt to have two things happen at once for very low CPU cost.

So the *effect* of using a post-increment is this:
-- Get the value of the variable (or element) that the operator refers to.
-- Hold on to that value.
-- Increment the variable (or element).
-- Retrieve the value that was held onto.

It was typically implement in machines of that time with code such as:
-- Load Y into the accumulator
-- Push the accumulator on the stack
-- Increment the accumulator
-- Store the accumulator into Y
-- Pop the accumulator off the stack

So, if y is 5, then doing:
Code:
`x = y++;`
will assign 5 (the original value of y) to x *AND* will bump the value of y to 6.

Notice that I said "variable or element".

The code applies to array elements and object fields as well:

Code:
```var ar = [0,1,2,3,4];
var x = ar[2]++;```
Result: x will be 2, ar[2] will be 3!

Anyway, this all dates back to AT LEAST the beginnings of the C language, since the original K&R book was published in 1978.

(And, yes, I really helped write a C compiler in 1980 and yes, we really did use the machine code shown above to implement the post-increment operator. And the "accumulator" was all of 16 bits long.)

• p.s.: Now go try it all in C# and see that it's the same there.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•