-1

I have a piece of code as this :

function imp(user) {
  return console.log("user", user)
}

var msg = imp("Jenny")

console.log("msg", msg)

I'm getting an output as this :

user //for user log
undefined // for msg log

However I expect this undefined if the code was async, for example a setTimeout call inside the imp function as :

function imp(user) {
  setTimeout(() => {
    console.log("inside the timeout for ", user)
  }, 3000)
}

But since the very top code is sync, then still why it logs undefined ?

Isn't that equal to simply assigning a new variable (msg) to a function call?

Thanks for reading

David
  • 208,112
  • 36
  • 198
  • 279
anshul
  • 661
  • 9
  • 27
  • 1
    `return console.log` will return undefined – evolutionxbox Sep 13 '22 at 10:57
  • why is that? It also returns undefined even I just do console.log without the return keyword – anshul Sep 13 '22 at 10:58
  • 2
    exactly. the return value of `console.log(...)` is undefined. – evolutionxbox Sep 13 '22 at 10:58
  • but even when I'm not using the return keyword? – anshul Sep 13 '22 at 10:59
  • 2
    @anshul: What value do you *expect* `msg` to have and why? – David Sep 13 '22 at 11:00
  • 1
    Please read [Does every Javascript function have to return a value?](https://stackoverflow.com/questions/17337064/does-every-javascript-function-have-to-return-a-value) – evolutionxbox Sep 13 '22 at 11:01
  • @David I want it to either return the function signature itself if I'm logging just `msg` or call the function with the `console.log(...)` inside it when I'm calling as `msg()` – anshul Sep 13 '22 at 11:01
  • 1
    functions return by default `undefined` if you dont use the `return` keyword – bill.gates Sep 13 '22 at 11:01
  • 1
    @anshul: If you want `msg` to be the `imp` function then it would be: `var msg = imp;` You're *invoking* the function. – David Sep 13 '22 at 11:02
  • @bill.gates So I believe that's a tricky slope here, if I don't use return keyword, then it would log undefined, if I do then console.log returns undefined by default? – anshul Sep 13 '22 at 11:03
  • @David Yes, exactly that's what i'm trying to do, but instead passing a user value as well as - `var msg = imp("Jenny")` – anshul Sep 13 '22 at 11:04
  • console.log returns undefined https://developer.mozilla.org/en-US/docs/Web/API/Console/log?retiredLocale=de#return_value – bill.gates Sep 13 '22 at 11:05
  • 1
    @anshul: Are you looking for this?: `var msg = () => imp("Jenny");` ? – David Sep 13 '22 at 11:05
  • @David yes, the same functionality as arrow signature, but can't we do it without an arrow function? It's just simply assigning a function call with params to a new variable – anshul Sep 13 '22 at 11:07
  • 1
    @anshul: Because, again, you are *invoking* the function. That's what using parentheses after a function name does. When you call `imp("Jenny")` you invoke the `imp` function and pass it the parameter `"Jenny"`. If you're not expecting this to invoke the function then why do you expect `console.log("msg", msg)` to invoke the `console.log` function and not just return a reference to the `console.log` function? Why would you expect that using parentheses does one thing in one case and another thing in another case? Invoking a function is basic JavaScript syntax... – David Sep 13 '22 at 11:10
  • @David I understood, Thanks for the description. So If I do - `var msg = imp;` & later do `msg('jenny')` then it would invoke the function correct? Please put the comment as an answer. I would tick it off if you like. – anshul Sep 13 '22 at 11:13

1 Answers1

2

The issue is the return of the console.log(), a console.log() returns undefined and not the message it just logged.

https://developer.mozilla.org/en-US/docs/Web/API/console/log

Pascal
  • 342
  • 1
  • 7