Incrementing Values

Joined
Feb 15, 2021
Messages
99
Reaction score
0
If you only want to increment a value by 1, you can use the ++ operator. This goes either directly before or after the variable:

ok. got it.

need help here, please

So what’s the difference between putting the ++ operator before or after the variable? The main difference is the value that is returned by the operation. Both operations increase the value of the points variable by 1, but points++ will return the original value then increase it by 1, whereas ++points will increase the value by 1, then return the new value:

Copypoints++; // will return 6, then increase points to 7<< 6
++points; // will increase points to 8, then return it<< 8

i tried this


Code:
<script>
let points = 5;
++points;
alert(points);
    </script>

and

Code:
<script>
let points = 5;
points++;
alert(points);
    </script>

both came back as 6

confused, please help
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
The difference is what is returned.
Look at this:
Code:
<script>
i=1;
j=++i;   
alert("i is "+i+" j is "+j);
</script>
i and j are both 2

But look at this:
Code:
<script>
i=1;
j=i++;   
alert("i is "+i+" j is "+j);
</script>

Now you see the diff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top