1

I am trying to increment a value by one each time but its not working. My variable currentValue gives me 0 as expected but when i try increment it using var newValue = currentValue ++; it still gives me 0.

function updateClicks(e, maxValue) {
    var buttonClicked = e.target;
    addClass("clicked", buttonClicked);
    var counterBox = buttonClicked.nextElementSibling;

    var currentValue = buttonClicked.value;
    console.log(currentValue);

    var newValue = currentValue++;
    console.log(newValue);
}

What am I doing wrong here?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
red house 87
  • 1,837
  • 9
  • 50
  • 99
  • 2
    `newValue = currentValue++` effectively does: `newValue = currentValue; currentValue = currentValue + 1;`. Try `newValue = ++currentValue;`, or just `newValue = currentValue + 1` if you don't actually want to change `currentValue`. – Niet the Dark Absol Jul 21 '16 at 18:07

3 Answers3

2

If you want to affect the incremented value you have to use pre increment operator like :

var newValue = ++currentValue;

Since currentValue++ (post increment) will increment the currentValue but after assigning it to the newValue variable.

  • pre-increment : (++currentValue) will adds one to the value of currentValue then returns currentValue.
  • post-increment : (currentValue++) will returns currentValue then adds one to it.

Hope this helps.

var currentValue = 0;
console.log('Increment using pre increment : '+(++currentValue));
console.log('pre increment return : '+currentValue);

console.log('-----------------------------------');

var currentValue = 0;
console.log('Increment using post increment : '+(currentValue++));
console.log('post increment return : '+currentValue);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

It will give you 0 for first time. Why ?
++ (post increment) operator - returns the value then increments it's value. Example

var a = 4;
var b = a++; // Here it assigns 4 to b, then increments it's value
console.log(a); // 5
console.log(b); // 4

You must use the pre increment form

  var a = 4;
  var b = ++a; // Here it increments it's value then assigns it to b
  console.log(a); // 5
  console.log(b); // 5
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

try:

var newValue = ++currentValue;
Sergeon
  • 6,638
  • 2
  • 23
  • 43
  • A little explanation would be helpful. What is the problem and how will this help? – showdev Jul 21 '16 at 18:59
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 22 '16 at 02:36