Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the value plus plus at last is 100?

when i use ++ operator in javascript, why I get the value at last is 100 not 101?
I want to know the detail of the ++ operator in javascript?

let value = 100;
value = value++;
console.log(value); // 100 why the value is 100 at last

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Because when you have the assignment operator, the right-hand operand is evaluated first, then that value is assigned to the receiver on the left.¹

So here’s what happens in value = value++:

  1. Read the value of value (100) and set it aside (since we’ll need it in a minute).
  2. Increase the value of value by one (making it 101).
  3. Take the value from #1 (100) as the result value of the right-hand operand.
  4. Store that value in value.

¹ In specification terms it’s slightly more complicated than that, but the details aren’t important here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading