DEV Community

codingTheWorld777
codingTheWorld777

Posted on

Know more about function in JavaScript

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.

Top comments (9)

Collapse
 
sargalias profile image
Spyros Argalias

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 using console.log because we don't know. We would have to look inside readlineSync.

If you add return call(getName), it returns the result up to where you have question(). If you don't add return, it returns undefined instead. So in other words:

function foo() {
  return 5;
}
let result = foo(); // result is 5

function bar() {
  const obj = {};
}
let barResult = bar(); // barResult is undefined, because we didn't return anything in the function

Hope that helps. Please feel free to ask if you would like me to explain more.

Collapse
 
codingtheworld777 profile image
codingTheWorld777

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?

Collapse
 
sargalias profile image
Spyros Argalias

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 :)

Collapse
 
codingtheworld777 profile image
codingTheWorld777

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()". )

Collapse
 
codingtheworld777 profile image
codingTheWorld777

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() @@.

Collapse
 
sargalias profile image
Spyros Argalias

Sorry but I don't fully understand just from the description. Did you have a link to the code? :)

Thread Thread
 
codingtheworld777 profile image
codingTheWorld777

Oh sorry. Here is the link: repl.it/@doHoangKhanh121/StrikingU...

Thread Thread
 
sargalias profile image
Spyros Argalias • Edited

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 uses return.

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:

const result = callback(); // this will always be undefined because we're not returning anything from inside callback()

For an example of how return works please see my previous reply :).

Hope that helps :)

Thread Thread
 
codingtheworld777 profile image
codingTheWorld777

Yep. That really helps me to know more about "return" in function. Thank you very much 🤗