user12682620
user12682620

Reputation:

Why does this print an extra 'undefined'?

Why does this code :

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

prints Toot Undefined

Where does undefined comes from?

Upvotes: 0

Views: 53

Answers (2)

Murat YILDIRIM
Murat YILDIRIM

Reputation: 49

If you want log in your function, just call horn.

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

Upvotes: 0

feedy
feedy

Reputation: 1140

You are trying to print the return (result) of the function. Even though your function does something inside, it has no return statement (aka doesn't send a result back), therefore you get undefined. If you want to get only 1 "Toot", try this:

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

Upvotes: 1

Related Questions