itemCount and orderCount


#1

I understand that itemCount is a placeholder. A local variable.

But is it simply ‘replaced’ by OrderCount when the function is called here:

console.log(getSubTotal(orderCount)); or is it an ‘earlier’ function

Is the value of itemCount assigned to orderCount or is it vice versa?

I’ve noticed that several people have been confused by itemCount.


#2

the value of the argument at function call (orderCount) get passed to the function parameter (itemCount), so under the hood, if it helps, this is what happens:

itemCount = orderCount

which in programming terms is assigning the value of orderCount to itemCount. This is happening when the function is called

the ironic thing is, nearly no one has problems earlier in this track when we build takeOrder function, where we use exactly the same thing (parameters and arguments)


Isn't itemCount uneccessary?
#4

I just had this exact question. And unfortunately i’m still not really understanding how the program knows to replace orderCount with itemCount?


#5

i explained in much more depth here:

there isn’t a replace action? The argument at function call passes it value to the function parameter, JS is handling this


#6

look, it was hard to grasp for me too.

const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
};
  • ItemCount is just a place holder, so it could be anything
console.log(getSubTotal(orderCount));

you can see that when the function getSubTotal called inside the console.log, there is orderCount as a paramater. The orderCount is basically replacing the place holder aka itemCount and passes his value to the getSubTotal function (the value is 3 of course). And that’s how the computer knows to “replace” itemCount with orderCount. I hope that my explanation help you. :grinning:


#7

Okay, that makes a bit more sense. so you could even put like ‘puppiesAreAmazing’, instead of ‘itemCount’. but if that’s the case and it’s just holding the place of orderCount, why don’t we just write orderCount? Why do we have to implement a placeholder parameter? I don’t really see the purpose.


#8

Hi @shearmadness, if you don’t mind me chip in a little while waiting others to reply, I hope to share my thoughts on this subject which I wrote some time ago, it was quite long, but some parts of it touched upon your doubts and question. If you have some time to spare, you can refer it here (Disclaimer: the examples there were using ES5 syntax, not the latest ES6).

@stetim94 does bring up a very good point about the irony of this matter:

I once cataloged few @stetim94’s answers about this ‘itemCount’ matter for another user, you can read it here if you’re curious about what others have been asking too.

Hope it helps.


#9

using orderCount as a parameter will work too. However, it is best avoid using orderCount or any same Name as a parameter when it exists as a variable itself. Is to reduce confusion or accidental error.


#10

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