Hello everyone,
I met a problem in this case. I try to execute the "call" function by involving another function (in this case "question". In "question", I want to call the "call" function inside "question" to get the finally result that is "Hello" + name, but it didn't work and when I add "return" before the line "call(getName)" or console.log it, the result can be displayed, and I don't really understand WHY. Can anyone explain me?
Here is the link: https://repl.it/@doHoangKhanh121/Pratique-JS
Thanks a lots.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (9)
Okay so you know what to do to make it work, but not why that works. I'll try to explain.
To actually display something on the terminal, you need to use
console.log
. So as you say, that's why it logs to the terminal when you use it.readlineSync
is different. That is a library you're calling so that could work in any way. Let's not worry about why that outputs to the terminal without usingconsole.log
because we don't know. We would have to look insidereadlineSync
.If you add
return call(getName)
, it returns the result up to where you havequestion()
. If you don't addreturn
, it returnsundefined
instead. So in other words:Hope that helps. Please feel free to ask if you would like me to explain more.
Hi. It's me again. Actually I have thought about the order of return in JavaScript last night. Let's comeback to the first example. Because I actually called the "call" function inside "question" and then involve 'question' at global scope. My opinion is that when I execute 'question' outside, this function does its work, after all because I didn't return it so the final result of question() will be 'undefined', but I actually call 'call' inside and it returns a value. It makes me think that the final result of question() is the 'return value' of "call" (actually it's not :) ). So the question here is which one came first? The return of 'question' or the return of 'call'? Or I have a wrong question here, it doesn't mean the order of return?
Hmm... I think from the previous replies I've given you should have your answer. I understand these things are difficult to understand at first, but at this point I would suggest checking out a resource that can explain it better than me and in more detail.
Generally recommended resources are:
Good luck and keep on learning :)
Yeah I see what you mean. But let's look around an another example here. It looks similar to the first one, but when you run thà program, it can run by anyway (although we don't "return" "multiplication()" inside the "callBack()". )
I am so confused that I don't need to return the "multiplicationTable()" inside "callBack()" to involve the "callBack()" but finally I can receive the result from return of multiplicationTable() @@.
Sorry but I don't fully understand just from the description. Did you have a link to the code? :)
Oh sorry. Here is the link: repl.it/@doHoangKhanh121/StrikingU...
So in this program we're not receiving results or returning anything. We're only printing to the terminal because we use
console.log
.Printing to the terminal and returning a result are separate things. One uses
console.log
and the other usesreturn
.In this program, we print to the screen on line 7 where we have console.log(
${num} * ${i} = ${num * i}
)But we never return anything, so on line 17, if we do:
For an example of how
return
works please see my previous reply :).Hope that helps :)
Yep. That really helps me to know more about "return" in function. Thank you very much 🤗