Understanding this unnecessary const addition to code

This question isn’t about return but rather the example with that exercise:
solutions is:

function monitorCount(rows, columns){
  return rows * columns
}
const numOfMonitors = monitorCount(5, 4)
console.log(numOfMonitors)

my question is why do we need the 'const numOfMonitors in the first place when

function monitorCount(rows, columns){
  return rows * columns
}
console.log(monitorCount(5, 4))

returns the same 20 that we’re looking for AND what diff would it make to say numOfMonitors vs monitor count in the first place?

1 Like

Hey there!

Two things:

  • Logging and returning are two different ideas. What is logged is just displayed on a terminal, but it’s not retained by the computer in any tangible way. The lesson probably wants to display how values can be stored in variables for later use. Usually, if we go through the trouble of writing a function for something, it’s because we need the value returned (in a lot of real-use cases).

  • numOfMonitors is the same as naming it monitorCount but it has to do with convention (which will vary depending on who you work with). In my personal opinion a count is better used for things describing loop cycles, and num is better for quantities – but it’s not a hard and fast rule. It’s just trying to be semantically clean.

Thanks, but I’m afraid that isn’t my question. I understand the difference between console.log and return. (I actually hate that all these courses use console.log [which I understand you have to have for debugging] instead of showing how a function would ultimately work in a working program.)

My question is why would we need to create the const numOfMonitors in the first place

why wouldn’t

function monitorCount(rows, columns){
return rows * columns
}
monitorCount(5,4) 

This seems to generate the same thing in the console (although because I can’t see the actual return , as I pointed out) I’m not sure if it actually does.

Seems like the additional variable serves no purpose.

And I did find that also when I went to the next lesson and added total cost. the numOfMonitor seems to serve no purpose.

Hope that is clearer.

No problem.

But the thing is that let’s say this is a real program. When you call monitorCount(5,4) what happens now? Nothing really. Your program won’t remember what the amount is.

To understand why you would want to store it in a variable, you’d have to think what other functions would play along with it. constructMonitor() should probably add one. Sell monitor should probably subtract one (and potentially add it to the client’s count).

The additional variable serves no purpose in this context, but if this type of function is written to begin with, it implies that it will be part of a bigger chain. Hence the need for some “container”.

@vincecaruso As for the lessons, they’re small model examples. If they went too specific they’d distract from the main conceptual points (it’s a pedagogical choice). There’s a great (free) book you can use for reference on good theory https://eloquentjavascript.net/. It goes more in-depth on the little things, which means it’s not easy to read quickly (and it shouldn’t really be). But you’ll get a better sense of the structural forces at play.

Thanks… I had heard of that book, and it was on my list of “to get”… still reading “javascript for kids” (which doesn’t seem like it’s for kids, but it is easier to understand); then I’ve got “head first” and then I was going to track that eloquentjavascript down… So thanks for the input and link.

1 Like

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