0

When ever I am trying to write a simple arrow function in ES6, I get the output right but also get an undefined in the result.

const greet = (name) => {

 return `Hello ${name}`;
};

console.log(greet("Nagendra"));

This is the output, running it for the first time gave undefined as result and running twice, gave the actual result

Nagendra Singh
  • 47
  • 1
  • 4
  • 10
  • 1
    `undefined` is just the return value of `console.log()` itself. – VLAZ Jan 23 '20 at 17:38
  • Please do not paste a screenshot of code, copy paste the code directly in your question using code block. – Mickael B. Jan 23 '20 at 17:39
  • Hey Michael, I pasted the code as well , screenshot is just to show the behaviour of output – Nagendra Singh Jan 23 '20 at 17:49
  • @VLAZ: Thanks VLAZ, though I understood the concept behind it, yet I was stumped when the same behaviour was in the online editors too. Do those follow the same concept? – Nagendra Singh Jan 23 '20 at 17:51
  • Yes, if you use a REPL of some sort, the same would happen. Ultimately a REPL (including the browser console) will run some code and print the return value. `console.log` also prints. So, you get `console.log("hello world")` -> print "hello world" (from inside `console.log`) -> print `undefined` (from evaluating `console.log()` – VLAZ Jan 23 '20 at 17:56
  • In addition to the statements @VLAZ suggests, compare just the statement `greet("Nagendra")` (without `console.log`) which causes the return value of your `greet` call to be displayed directly. In your case, it prints the return value of `console.log`, which is always `undefined`, in addition to logging `log`'s argument to the console. – apsillers Jan 23 '20 at 18:20
  • Thanks all. Understood!!1 – Nagendra Singh Jan 23 '20 at 19:09

0 Answers0