Booklet

author

Hack University

IT & Software

Difference between i++ and ++i

DIFFERENCE BETWEEN I++ AND ++I

Both i++ and ++i

Result in i being increment by 1. However, the increment operation occurs at different times during the evaluation process.

var i = 0;
var a = i++; //the value of a is equal to 0

This is a postfix increment operator. In this case, the current value of i is first returned and set to a, giving a the value of 0. Then i is incremented by 1.

var i = 0;
var a = ++i; //the value of a is equal to 1

This is a prefix increment operator. In this case, the current value of i is first incremented by 1 and then set to a, giving a the value of 1.

AN EASY WAY TO REMEMBER THIS IS....

If the ++ is before the variable, then we first increment and then return the value.

If the ++ is after the variable, then we first return the value and then increment.

More booklets by Hack University

More IT & Software booklets

Your recently viewed booklets

  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
  • booklet-slide-images
See all booklets