DEV Community

Cover image for Js Devs: Position of you ++ or — matters
Chocoscoding - Oyeti Timileyin
Chocoscoding - Oyeti Timileyin

Posted on

Js Devs: Position of you ++ or — matters

JavaScript provides powerful operators for manipulating numeric values ++ for incrementing and -- for decrementing. Understanding the nuances between postfix and prefix usage is crucial for precise programming.

Postfix Increment and Decrement

Postfix increment and decrement operators (i++ and i--) are used to change the value of a variable after its current value is used in an expression.

let i = 5;
console.log(i++); // Outputs: 5 (uses current value, then increments)
console.log(i);   // Outputs: 6 (value has been incremented)
Enter fullscreen mode Exit fullscreen mode

Prefix Increment and Decrement

Prefix increment and decrement operators (++i and --i) are used to change the value of a variable before its current value is used in an expression.

let i = 5;
console.log(++i); // Outputs: 6 (increments first, then uses the updated value)
console.log(i);   // Outputs: 6 (value has been incremented)
Enter fullscreen mode Exit fullscreen mode

An extra example

  • With postfix
// Postfix decrement: prev--
let inventoryCount = 5;

function processSalePostfix() {
  if (inventoryCount > 0) {
    console.log("Item sold! Remaining inventory:", inventoryCount--);
  } else {
    console.log("Item out of stock!");
  }
}

// Simulate sales
processSalePostfix(); // Output: Item sold! Remaining inventory: 5
processSalePostfix(); // Output: Item sold! Remaining inventory: 4
processSalePostfix(); // Output: Item sold! Remaining inventory: 3
Enter fullscreen mode Exit fullscreen mode

In this scenario, with postfix decrement, the current inventory count is logged, and then the inventory count is decremented. This might be useful if you want to display the current inventory count before processing the sale.

  • with prefix
// Prefix decrement: --prev
let inventoryCount = 5;

function processSalePrefix() {
  if (--inventoryCount > 0) {
    console.log("Item sold! Remaining inventory:", inventoryCount);
  } else {
    console.log("Item out of stock!");
  }
}

// Simulate sales
processSalePrefix(); // Output: Item sold! Remaining inventory: 4
processSalePrefix(); // Output: Item sold! Remaining inventory: 3
processSalePrefix(); // Output: Item sold! Remaining inventory: 2
Enter fullscreen mode Exit fullscreen mode

In this scenario, with prefix decrement, the inventory count is decremented before checking if it's greater than 0. This might be useful if you want to ensure that the inventory count is updated before any other operations, such as logging or further processing.

Conclusion

In summary, postfix increment/decrement is useful when you need the current value before the change, and prefix increment/decrement is ideal when you need the updated value after the change. This nuanced understanding will undoubtedly enhance your proficiency as a JavaScript developer.

NOTE FOR REACT DEVELOPERS: Be careful with postfix if your values may be used before a rerender.

Thanks for your time 🙂👍.

Top comments (0)