The different between the add expression

Tell us what’s happening:
I was stuck at this challenge for about 2 days and I finally figure out solution trying lot of things, but I am still confused that when I trued const arr=rangeOfNumbers(startNum++, endNum); it didn’t work so I was wondering about what is the different between startNum++ , startNum+1 , startNum+=1 , ++startNum
please answer my question
kind regards.
Your code so far

function rangeOfNumbers(startNum, endNum) {
      const arr=rangeOfNumbers(startNum+1, endNum);

   return arr; }else{  return [];}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

startNum is a location in memory.
Saying startNum+1 means you want to fetch the number stored in startNum and add 1 to it. But it doesn’t store that value back in memory. It just produces a new number in a different part of memory.
startNum remains unchanged in this current scope.

startNum+=1 is syntactic sugar equivalent to writing startNum = startNum + 1;
Obviously this will modify the value of startNum in memory (that’s what the assignment operator does) In this scope startNum becomes 1 larger than it used to be.

The other too are also syntactic sugar. And also modify the value in memory.
Though one modifies it before being passed and the other after.
++startNum and startNum++ are the same in the current scope in your case though. They are different for the rangeOfNumbers produced though. (As the prefix version will start the range one bigger than the postfix one I believe)

Hope this helps somewhat.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.