Generally speaking, we don’t have functions purely for the purpose of logging results, though that is just fine. One would expect that the returns from our functions have some further use, hence the assignment of the return value. We can use it again, as many times as we need.
The example above merely logs the return from the function, and nothing else. If that’s all we want, then no problem. If we want to use that result, it has to be assigned, or at least passed to another function while it is intact (much the same way we passed it to console.log()).
Bottom line, don’t look for reasons NOT to use code patterns. Learn how to ideate the code patterns in your problem solving. There is no baby if it goes out with the bathwater. Save some of your questions/doubts for when you can answer them yourself. It is inevitable if we keep up our learning progress (and do the background reading).
Thanks for taking the time to reply to my question. I am really trying to understand your explanation but still don’t see the need to include a completely new constant in the form of numOfMonitors when we can easily call the already-defined function name monitorCount and pass IT the arguments of 5 and 4 and then log it to the console. Like so:
The same result will still be achieved if you leave out numOfMonitors and just continue to use monitorCount. I understand that you CAN make one variable equal a predefined function in which you pass IT certain arguments. I understand that there may be situations (probably is the norm rather than the exception) where the coding may get more complicated and will necessitate the need for such methodology but for this simple exercise I still don’t see the need to bring in a completely new const numOfMonitors. That is what is confusing to me.
Therefore, is it necessary/best practice to bring in a new constant/variable and assign it a predefined function to make use of the function when we want to call it or is this methodology simply showcasing one of many different ways to call upon a function? Or is it best practice to NOT directly call upon a function but instead call upon it via another variable/constant?
In any event, it is good to know both methods will get the same result. So, I understand the “how” but don’t understand the “why” part of the solution given to us which I feel CodeCademy could elaborate a little more on. Perhaps you can shine a bit more light there. Much appreciated!
EDIT
Reviewing an answer you gave to Jon Morris last year…I think you answered my question. Just to make sure I am getting this correct–numOfMonitors is needed so that the value of “20” may be stored in memory for further manipulation. Whereas, if we directly use monitorCount and call upon it, the value of “20” is returned but not stored for future use. Is this correct? If so then I definitely can see why a const/variable would need to be used to call upon a function. If correct, then I don’t think Codecademy did a thorough job on explaining this very important aspect of function use…or at the very least didn’t emphasize it enough which is why many keep asking similar if not the same question.
Thank you though mtf–the explanation you gave Jon Morris really helped if indeed I am correct in my understanding.
Again, if all want is to log the result and move on, then clearly we do not need a variable. It is only necessary if we need to access the return value at some later point in the program. We would only declare it as a const if we want it to be persistent and immutable. It is common to re-use a variable, perhaps as the return from the same function each time, in which case we would declare it with, let, thus permitting mutation.
There are not different ways to call a function, only how we work with the return. I would not try to read too much into this, or take issue with what may appear to be pointless or redundant. This is really so much splitting of hairs. Take what you can from the lesson and continue on to the next. In due course of time more will be revealed from your work. Nothing is carved in stone, at this point, nor really ever will be, when we come down to it. Diversity and variation is a very common theme in programming. If we stop to question every little detail we are bound to get buried in detritus.
Thank you mtf. I understand the log part and I believe I understand the reason for using the const in this example. It is what you further reiterated. It is important to me that I understand why though…Javascript is a completely different animal than html and css. Really having a hard time just understanding the basics and syntax. Thank you for your advice on just moving on too. I feel like I am bogged down in a Javascript swamp.
JS is a whole 'nother animal when compared to HTML and CSS, which while they are languages, of a sort, they are not programming languages, but declarative in nature. They do not have any logic.
Something to note, a value stored in memory which has no reference is not reachable and the system marks it for garbage collection to free up memory. At this stage the last thing we want is to understand JavaScript in technical terms. Focus on the syntax and general usage of each new concept with a ‘scratch the surface’ point of view. Just take it in, and for now, take it in stride that you will not understand everything right off the bat. You won’t. Accept that and just keep an open mind.
I would also forget about what progress you are making and repeatedly loop back to the beginning and go over everything you have learned to that point. Be sure to put in the practice time, and try some experimenting while you’re at it. Knowing that something works or doesn’t work is actually enough for now, without having to know the real how and why. Review the documentation so you learn what the language promises to do, work with that promise in mind and gradually it will sink in to the point that a lot of your questions get answered and you are able to peer deeper into the inner workings of the language.
Concepts are always confusing to learn, at first. If they weren’t everyone would be a programmer. It is not for everyone. Letting yourself get too deep into the weeds will erode your confidence and make it difficult to learn. Our goal is to stimulate confidence by working through our confusion and not moving forward while it persists. Learn to love confusion, mistakes and errors in one’s thinking and logic. By continuing to read and ask questions you are on the right path.
is used this and i still get the same result what can you say about this kind of code
function nums(num1,num2) {
const result = num1 * num2;
return result;
}