Daniel Tea


This one always trips me up. If the ++ increment is after the operand, then it's called a post increment (i++). If the ++ is before the operand, then it's called a pre increment (++i).

With the post increment, the value gets incremented by one, and returns the original value.

let i = 2
let j = i++

console.log({ i, j })
// { i: 3, j: 2 }

With the pre increment, the value gets incremented by one, and returns the new value.

let i = 2
let j = ++i

console.log({ i, j })
// { i: 3, j: 3 }

They're commonly seen in for-loops where it generally doesn't matter which one is used.

for (let i = 0; i < 3; i++) {
  console.log(i) // outputs 0, 1, 2
}

for (let j = 0; j < 3; ++j) {
  console.log(j) // outputs 0, 1, 2
}