Monday 15 August 2011

increment - Difference between ++ and +=1 in javascript -


Can someone explain why the following functions give different results? First does not seem to work, but the second makes me wonder because I thought that + = 1 and ++ did the same thing

(I really intend to use this code I'm not, it's just for displaying the difference).

  / * function 1 * / function incrementIfZero1 (base, element) {If (element == 0) {return base ++; } And {return base; }}; / * Function 2 * / Function increment IFZero2 (base, element) {if (element == 0) {return base + = 1; } And {return base; }}; Increment IFZero1 (1,0) / * - & gt; 1 * / Salary increment Eiffel 2 (1,0) / * - & gt;  

Thanks

/ P>

Thank you for your reply, it is understandable now I have also tried the following statement, which resulted in 1:

  return (base ++)   

Now I wonder if this function 2 Does not give the same result - I expected that brackets should evaluate 'force' before returning. Why not think of any such thing?

When you send back to base ++ before it grows The value of the base comes back. ++ As is the case with +, increments increase first

= 1

In response to your edit, I tried to wrap random statements in parentheses and most mathematical operators responded as expected, this increase is being free, because possibly the syntax of the pre-variance versus Post-incrementality is highly deliberate and only in the statement returns Neither is a specific value or does not you wrap it in brackets

No comments:

Post a Comment