-2

const horn = () => {
  console.log("Toot");
};
console.log(horn());

I am getting the output as

Toot undefined

But I can't understand why it is so

Mohammad
  • 20,339
  • 15
  • 51
  • 79

4 Answers4

6

Your horn function does not return anything ...

const horn = () => {
  return 'horn';
};
const horn2 = () => {
  console.log('horn');
};
console.log(horn());
horn2();
messerbill
  • 5,142
  • 1
  • 23
  • 35
Davy
  • 6,094
  • 5
  • 26
  • 36
3

return

If the value is omitted, undefined is returned instead.

Your function does not return anything. If a function does not return anything then by default undefined is returned.

const horn = () => {
  console.log("Toot");
  return "Toot";
};
console.log(horn());
Mamun
  • 62,450
  • 9
  • 45
  • 52
0

Because you're function is not returning any values.

const horn = () => {
  console.log("Toot");// <-- problem
};

It should be like this

const horn = () => {
  return "Toot";
};

console.log(horn());

Ropali Munshi
  • 2,287
  • 3
  • 20
  • 31
0

undefined can be remove if you return something in horn() function

const horn = () => {
      return "Toot";
    };
console.log(horn());
Ayaz Ali Shah
  • 3,305
  • 8
  • 35
  • 64