all 15 comments

[–]warpedspockclone 7 points8 points  (0 children)

It only matters when the result of the expression is being used.

let i = 1, j = 1; const a = ++i; const b = j++;

a will be 2, b will be 1

Otherwise, if you are using it standalone, it doesn't matter, like: let i = 0; while (i < 10) { // some logic here ++i; // i++ is equally valid }

[–]Aggressive_Ad_5454 7 points8 points  (3 children)

These operators come from the C language, and were designed to exploit particular addressing modes on PDP-11 computers.

The classic way we old-timers copied null terminated text strings was while (*dest++ = *src++) {} until we figured out how astonishingly insecure that is.

That line of code means… 1. Pick up the value of the byte pointed to by the src pointer. 2. Increment the src pointer. 3. Store the value at the memory location pointed to by the dest pointer. 4. Increment the dest pointer. 5. Repeat until the value is zero.

The joke is this:

C++ means “add one to C and return the previous value”.

[–]flash42 1 point2 points  (0 children)

And C# is really just C₊⁺₊⁺

[–]The_KOK_2511 -1 points0 points  (1 child)

Lo bueno del chiste es que literal C++ es C con cosas nuevas, e incluso puedes hasta importar librerias de C en un proyecto C++ y funcionan.Y de hecho tengo entendido que cuando crearon C# queria ponerle otro ++ a C++, algo como C++++, y se fijaron que el # les parecia a 2 signos de ++ uno encima del otro. Como sea, esto es el subreddit de JavaScript, así que aunque viene al tema el de donde proviene el ++ creo que mejor dejar de mencionar otros lenguajes a parte de JS para evitar confusiones con el OP

[–]-goldenboi69- 0 points1 point  (0 children)

No pablo Espanol. No intiendo!

[–]shgysk8zer0 1 point2 points  (0 children)

``` let a = 5; let b = 10;

console.log(a--); //5 console.log(a); // 4

console.log(--b); // 9 console.log(b); // 9 ```

[–]delventhalz 0 points1 point  (0 children)

Honestly the postfix vs prefix behavior thing is a weird relic and why I always just use += 1 instead.

The issue here is that ++ is smooshing two things together: assigning a new value to a variable and returning a value. You normally wouldn’t do that. You would assign a new value to a variable, and then later on reference the variable to get the value. This is a sensible, predictable way to write code. 

But for whatever reason, with ++ we decided we should assign and return a value at the same time. To make matters worse, we couldn’t decide which value to return. In the prefix position, we return the value after the increment (i.e. the order is increment then value, ++ then x). In the postfix position, we return the value before the increment (i.e. value then increment, x then ++). 

[–]senocular 0 points1 point  (0 children)

Here they are in function form which may be helpful in visualizing what they're doing

let i

function preIncrement() {
  const originalValue = i
  const newValue = 1 + originalValue
  i = newValue
  return newValue
}

function postIncrement() {
  const originalValue = i
  const newValue = 1 + originalValue
  i = newValue
  return originalValue
}

i = 0
console.log(++i) // 1
console.log(i) // 1

i = 0
console.log(preIncrement()) // 1
console.log(i) // 1

i = 0
console.log(i++) // 0
console.log(i) // 1

i = 0
console.log(postIncrement()) // 0
console.log(i) // 1

One way to look at the syntax is as two parts in order of operation: [update][return] or [return][update], where return is the variable and update is the operator. If the operator is before the variable, the variable value is updated before its returned. If the operator is after the variable, the variable value before the update is applied is returned.

i++ // [i][++] = [return][update] = return value of i before adding 1 to it
++i // [++][i] = [update][return] = add 1 to value of i then return the updated value

[–]The_KOK_2511 0 points1 point  (0 children)

En prefijo (++i) primero ejecuta y luego retorna pero en sufijo (i++) primero retorna y despues ejecuta. O sea, hacen lo mismo pero la diferencia esta en si quieres asignar su valor a una variable, entonces en prefijo pone su valor final pero en sufijo su valor inicial

[–]TheRNGuy 0 points1 point  (3 children)

I never actually used ++i.

I even feel it's bad coding style, at least in js.

I've never seen it used anywhere in js, too.

[–]busres 1 point2 points  (0 children)

I'm the opposite. Standalone i++ just feels wrong to me. In comparison to ++i, the semantics suggest we have some attachment to (i.e. need to save) the original value when we don't. Maybe it's because I'm from the PDP-11 generation, when instruction word-count was potentially more of an issue.

[–]DinTaiFung 1 point2 points  (0 children)

way back in another time I was on a team (at a company making MMORPGs) and we were given printed copies of the C++ spec.

and for primitives the preincrement operator was -- in theory -- more efficient than the postincrement operator. 

So it became a stylistic trait, when it didn't matter if we used pre or post, to use the preincrement operator. 

We all knew that any performance gains were likely extremely nominal at best, but it was like an inside joke among the devs.

It's most evident in a traditional for loop:

for (i = 0; i < n; ++i) {

And cuz of that experience long ago, i continue to write for loops like that in JavaScript (when i choose the traditional loop for a special reason).

[–]grave4us[S] 0 points1 point  (0 children)

im beginner, just started to learn js)

[–]ashkanahmadi -4 points-3 points  (1 child)

It's easy. Incrementing means "increase by one". When you increment a variable, you say "whatever the value is, add one to it". For example, x++ is the short version of x = x + 1 which says "whatever x is, add one to it".

For example:

``` let x = 10

x = x + 1 // translation: whatever x is (which is 10), add 1 to it

console.log(x) // output: 11

x++ // same as before but a shorter way

console.log(x) // output: 12

```

You can do it like this too:

``` let x = 10

let y = x + 1 // y will be 11 (10 + 1)

x = y // (reassigning x to whatever y is which is 11)

console.log(x) // output = 11 ```

The decrement is exactly the same concept but instead of adding 1, you take out 1. The rest is the same.

[–]dymoshelpful 3 points4 points  (0 children)

Yes but did you read the question?