BFE.dev solution for JavaScript Quiz
7. Increment Operator

TL;DR

++a and a++ both increments, but returns different values. ++a returns new value while a++ returns the old one.

let a = 1
const b = ++a
// a is 2, b is 2
const c = a++
// a is 3, c is 2
console.log(a)
// 3
console.log(b)
// 2
console.log(c)
// 2

ECMAScript spec for Prefix Increment Operator

From the spec, we can easily understand how it works for ++a.

UpdateExpression : ++ UnaryExpression

  1. Let expr be ? Evaluation of UnaryExpression.
  2. Let oldValue be ? ToNumeric(? GetValue(expr)).
  3. If oldValue is a Number, then
    1. Let newValue be Number::add(oldValue, 1𝔽).
  4. Else,
    1. Assert: oldValue is a BigInt.
    2. Let newValue be BigInt::add(oldValue, 1ℤ).
  5. Perform ? PutValue(expr, newValue).
  6. Return newValue.

Simply the last line, we can see new value is returned.

ECMAScript spec for Postfix Increment Operator

From the spec, we can easily understand how it works for a++.

UpdateExpression : LeftHandSideExpression ++

  1. Let lhs be ? Evaluation of LeftHandSideExpression.
  2. Let oldValue be ? ToNumeric(? GetValue(lhs)).
  3. If oldValue is a Number, then
    1. Let newValue be Number::add(oldValue, 1𝔽).
  4. Else,
    1. Assert: oldValue is a BigInt.
    2. Let newValue be BigInt::add(oldValue, 1ℤ).
  5. Perform ? PutValue(lhs, newValue).
  6. Return oldValue.

We can see that the steps are basically the same as Prefix Increment Operator, except that at last old value is returned.

You might also be able to find a solution fromcommunity posts or fromAI solution.