0

I know that in C, C++, Java, etc, there is a difference between foo++ and ++foo. In JavaScript, there is no ++foo, and JavaScript's foo++ usually behaves like C's ++foo:

var x = 10;
x++;
console.log(x); //11

But here:

var x = 0;
var w = "012345";
console.log(w[x++]); //0
console.log(w[x++]); //1

...It behaves like C's foo++?

7
  • When placed after an expression, ++ is a Postfix Increment Operator. When placed before a unary expression, it's a Prefix Increment Operator. -- is the equivalent prefix/postfix decrement operator. Neither are unary operators. Commented Aug 30, 2019 at 1:28
  • JavaScript's foo++ usually behaves like C's ++foo - I think you made a mistake in this statement ... the correct version is JavaScript's foo++ never behaves like C's ++foo - or alternatively JavaScript's foo++ always behaves like C's foo++ Commented Aug 30, 2019 at 1:34
  • @JaromandaX then how do you explain the above result? ( Try it if you don't believe me) Commented Aug 30, 2019 at 2:18
  • the above results are correct. first code, x will be 11 before you console.log it ... the post-increment takes place then you console.log ... if you console.log(x++) you will get 10 not 11 - post-increment doesn't wait until an arbitrary time later to change x within the statement it is executed ... which is x++ Commented Aug 30, 2019 at 2:31
  • 1
    @RaphaelSpoerri—in the second case, x++ logs 0, then increments x. Then it logs 1 and increments x again (so next time it will be 2). Prefix increment means increment, then evaluate the expression. Posfix increment means evaluate the expression, then increment. In both cases, the value is incremented before the next expression is evaluated. The difference is the value used in the expression involving the operator. Commented Aug 30, 2019 at 3:33

2 Answers 2

3

In JavaScript, there is no ++foo

That's wrong. There is a pre-increment operator, and it works like this:

var x = 0;
var w = "012345";
console.log(w[++x]); //1
console.log(w[++x]); //2

Sign up to request clarification or add additional context in comments.

11 Comments

Oh, you' re right! that's so weird, because the reason most likely never seen ++x is because there is usually no difference, except for here.
Yes - in a normal for loop you'd usually use i++ rather than ++i (although for performance += is better).
can somebody, though, explain the weirdness of w[x++]?
There is no weirdness - it's equivalent to console.log(w[x]); x += 1;.
x++ returns the value of x, then increments x. ++x increments x, then returns the value of x. Logical given the placement of the operator.
|
0

With the help of Jack Bashford, I think I understand it:

The reason I asked this question is because I misunderstood what "increment after using" meant; I thought it meant at the next statement, it really means immediately after the expression. I also got confused because JavaScript has a few conventions about when to use ++. In conclusion, C's ++ and JavaScript's ++ work the same way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.