I’m learning about functions in JavaScript and I’m confused about the role of console.log()
and return.
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();
I understand this function logs “Hi World” to the console. My questions are:
-
Why doesn’t this function need a
return
statement? I’ve seen other functions that are usedreturn
to send values back. Isconsole.log()
doing something similar? -
Why do we call the function with just
reusableFunction();
instead ofconsole.log(reusableFunction());
? When would I use the second way of calling the function?
I’m trying to understand the fundamental difference between displaying something in the console console.log()
and returning a value from a function. Can you explain this with clear examples and perhaps point me to some resources for further learning?
I tried reading Documents like MDN, JS, and Google Developer Docs. I even did a Google Search and kept getting the basics. It answered my question. I just simply want to know when you know to use a return
statement in your functions and when to console.log()
and then call the function, like this console.log(reusableFunction())
. I understand what console.log() is what the return statement is and the difference between both.