• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / What is the difference between prefix and postfix operators?

What is the difference between prefix and postfix operators?

What is the difference between prefix and postfix operators?

The increment operator (++) adds 1 to its operand and returns a value. Similarly, the decrement operator (--) subtracts 1 from its operand and returns a value. Both of these operators can be used either prefix (++i, --i) or postfix (i++, i--).

If used prefix, the value is incremented/decremented, and the value of the expression is the updated value.

let i = 0;    // i = 0
let j = ++i;  // i = 1, j = 1
let k = --i;  // i = 0, k = 0

If used postfix, the value is incremented/decremented, and the value of the expression is the original value.

let i = 0;    // i = 0
let j = i++;  // i = 1, j = 0
let k = i--;  // i = 0, k = 1

Source

https://www.30secondsofcode.org/articles/s/javascript-prefix-postfix-operators

Miscellaneous

Related Snippets:

  • Return the last element in an array
  • Wrap an element around a given element
  • Vanilla JS toggle
  • Transform all text in a string to lowercase

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers