Solved

pre & post operands in javascript

Posted on 2011-03-08
3
266 Views
Last Modified: 2012-06-27
Greetings,

I want to make sure I understand the different results between the three code examples included of pre & post increment operands.

Example A:
i = 1;
j = ++i;

Example B:
i = 1;
j = i++;

Example A sets both i and j to 2 whereas example B sets i to 2 and j to 1.  
I want to make sure why j equals 1 in example B: I believe it is because the increment operand is post which means this code:

j = i++;

Is read like this:

j equals i which equals 1 (j = i)
Increment i so it equals 2 (i++)

Am I correct in my reading of the code from example B?  If I am correct, then I would like to apply this logic to the following code:

Example C:
var i = 1;
var j = ++i;  // pre-increment:  j equals 2; i equals 2
var k = i++;  // post-increment: k equals 2; i equals 3

Based on the code above, does k equal 2 because i was previously incremented in var j above it and the new value of i (2) is carried over to the code: var k = i++?  
0
Comment
Question by:max7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 350 total points
ID: 35073225
You are correct.  ++i means increment i then use the result's value.  i++ means use i's value then increment i.
0
 
LVL 18

Assisted Solution

by:Anil Golamari
Anil Golamari earned 150 total points
ID: 35073303
Yes you can use above code to get output as i =3, j= 2, k =2.
0
 
LVL 1

Author Closing Comment

by:max7
ID: 35073469
Thanks to you both for the help!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question