-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
  • 21,175
  • 15
  • 55
  • 84

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,499
  • 1
  • 27
  • 38
Davy
  • 6,295
  • 5
  • 27
  • 38
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
  • 66,969
  • 9
  • 47
  • 59
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,757
  • 4
  • 22
  • 45
0

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

const horn = () => {
      return "Toot";
    };
console.log(horn());
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68