JavaScript I++ VS ++I

javascript i++ vs ++i

The difference between i++ and ++i is the value of the expression.

The value i++ is the value of i before the increment. The value of ++i is the value of i after the increment.

Example:

var i = 42;
alert(i++); // shows 42
alert(i); // shows 43
i = 42;
alert(++i); // shows 43
alert(i); // shows 43

The i-- and --i operators works the same way.

What's the difference between ++i and i++ in JavaScript

++i returns the value of i after it has been incremented. i++ returns the value of i before incrementing.

When the ++ comes before its operand it is called the "pre-increment" operator, and when it comes after it is called the "post-increment" operator.

This distinction is only important if you do something with the result.

var i = 0, j = 0;

alert(++i); // alerts 1
alert(j++); // alerts 0

One thing to note though is that even though i++ returns the value before incrementing, it still returns the value after it has been converted to a number.

So

var s = "1";
alert(typeof s++); // alerts "number"
alert(s); // alerts 2, not "11" as if by ("1" + 1)

Difference between ++ and +=1 in javascript

when you return base++ it returns the value of base just before it gets incremented. You want to do ++base to make sure the increment happens first then it gets returned

otherwise ++ is the same as +=1

[edit] in response to your edit, i tried wrapping random statements in parentheses and most mathematical operators respond as expected, this incrementation seems to be exempt, likely because the syntax of pre-incrementation vs post-incrementation is highly intentional and the statement itself returns a specific value regardless of whether or not you wrap it in parentheses

i++ vs. ++i in a JavaScript for loop

In JS and PHP it does not make any difference, I think even in Java it does not make any difference but in pure c when compiler is not optimizing code it does, and that is why a lot of people use ++i because they are used to it from c.

EDIT:
This is an answer for JS if you want history of pre and post increment searc C/C++ pre/post increment. Or see comments on @Orvev's answer.

increment operator vs. i + 1 in loop

You are correct. MDN documentation for ++A says

The increment operator (++) increments (adds one to) its operand and returns a value.

which is unfortunate as it doesn't clarify what's going on and why use "pre" (or "post") to qualify "increment".

To pre-increment a variable means to get the value of the variable from memory, add one to it, store the result back in memory and return the addition result as the result of the pre-increment operator. It's called "pre" increment because incrementation occurs before returning the updated value of the variable to the program.

In contrast, to post increment a variable means to get the current value of the variable from memory, make a copy of it (perhaps in a CPU register), increment the value and save it back in memory - and then return the copied value the variable had before the addition took place. It called "post" increment because incrementation occurs after getting the value to return to the program.

Of course, (i+1) just gets the value of i, adds one to it and returns the result of the expression without updating the value of i.


A more precise version of the MDN article could include "l-value" in the glossary and describe the operator along the lines of:

The increment operator (++) is a unary assignment operator that increments its operand in storage and returns a result. The operand must be an l-value.

  • If ++ is used as a prefix operator, the incremented value of the operand is returned as the result of the operation.

  • If ++ is used as a postfix operator, the value of the operand before being incremented is returned as the result of the operation.

Is there a difference between ++i and i++ in this loop?

The only difference between i++, ++i, and i += 1 is the value that's returned from the expression. Consider the following:

// Case 1:
var i = 0, r = i++;
console.log(i, r); // 1, 0

// Case 2:
var i = 0, r = ++i;
console.log(i, r); // 1, 1

// Case 3:
var i = 0, r = (i += 1);
console.log(i, r); // 1, 1

In these cases, i remains the same after the increment, but r is different, i += 1 just being a slightly more verbose form of ++i.

In your code, you're not using the return value at all, so no, there is no difference. Personally, I prefer to use i++ unless there is a specific need to use one of the other forms.

++someVariable vs. someVariable++ in JavaScript

Same as in other languages:

  • ++x (pre-increment) means "increment the variable; the value of the expression is the final value"
  • x++ (post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"

Now when used as a standalone statement, they mean the same thing:

x++;
++x;

The difference comes when you use the value of the expression elsewhere. For example:

x = 0;
y = array[x++]; // This will get array[0]

x = 0;
y = array[++x]; // This will get array[1]

What is the difference between ( for... in ) and ( for... of ) statements?

for in loops over enumerable property names of an object.

for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.

In your example, the array iterator does yield all the values in the array (ignoring non-index properties).



Related Topics



Leave a reply



Submit