increment - Update value not incrementing in Javascript -


i trying increment value 1 each time not working. variable currentvalue gives me 0 expected when try increment using var newvalue = currentvalue ++; 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 doing wrong here?

if want affect incremented value have use pre increment operator :

var newvalue = ++currentvalue; 

since currentvalue++ (post increment) increment currentvalue after assigning newvalue variable.

  • pre-increment : (++currentvalue) adds 1 value of currentvalue returns currentvalue.
  • post-increment : (currentvalue++) returns currentvalue adds 1 it.

hope 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>


Comments