p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning JavaScript 5th Edition (http://p2p.wrox.com/forumdisplay.php?f=793)
-   -   Chapter 2 - post increment /decrement not working (http://p2p.wrox.com/showthread.php?t=98816)

Madongeer April 21st, 2016 11:58 AM

Chapter 2 - post increment /decrement not working
 
Hi, I've just started learning javascript with this promising book, but I got stuck in the increment and decrement part. The post increment /decrement not working with me:
HTML Code:

<script>
var myFirstVariable=1;
var result;
result=myFirstVariable++;
alert(result);
</script>

It shows the value without any change, that is 1
What am I missing here?
Thanks in advance

irfaan102 May 23rd, 2016 10:04 AM

He assign the value to the variable "result" first.
Then he increment the variable "myFirstVariable".

If you want to verify, if the variable was incremented just write another alert function under the first one.

<script>
var myFirstVariable=1;
var result;
result=myFirstVariable++;
alert(result);
alert(myFirstVariable);
</script>

If you use the prefix increment:
result=++myFirstVariable;

It will increment first, and then assign the value to the variable.


All times are GMT -4. The time now is 01:18 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.