最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - console.log(myFunction()) returns undefined - Stack Overflow

programmeradmin2浏览0评论

I'm new to JavaScript, and I try to play around with it to understand all in-and-outs. I write

function greet() {
    console.log("Hi");
};

console.log(greet());

And the result of it in the console is

> Hi app.js:2 
> undefined app.js:4

I assume this is because greet() inside console.log first calls the function, which prints out "Hi". We get first line of log. But where did the second line e from?

Then I thought because Hi is overall result of greet(), then console.log basically calls variable Hi, but in this case the result would be is not defined, not undefined

I'm new to JavaScript, and I try to play around with it to understand all in-and-outs. I write

function greet() {
    console.log("Hi");
};

console.log(greet());

And the result of it in the console is

> Hi app.js:2 
> undefined app.js:4

I assume this is because greet() inside console.log first calls the function, which prints out "Hi". We get first line of log. But where did the second line e from?

Then I thought because Hi is overall result of greet(), then console.log basically calls variable Hi, but in this case the result would be is not defined, not undefined

Share Improve this question asked Jan 21, 2018 at 0:27 Sergey PleshakovSergey Pleshakov 8,9982 gold badges23 silver badges42 bronze badges 3
  • The return value of greet() is undefined so that is what you see as the log of line 4. – rolfv1 Commented Jan 21, 2018 at 0:44
  • A function that has no specific return value returns undefined by default when executed. Check console.log((function(){})()) in your browser's console. – connexo Commented Jan 21, 2018 at 0:45
  • Hi is not the overall result of greet(), the function simply logs a message to the console. Looks like you actually want return "Hi"; Functions that do not have a return value specified return undefined. – skyline3000 Commented Jan 21, 2018 at 0:50
Add a ment  | 

3 Answers 3

Reset to default 8

In JavaScript, if nothing is returned from the function with the keyword return then undefined is returned by default.

var data = greet();
console.log(data);// undefined, since your function does not return.

Is equivalent to:

console.log(greet());

The second output is the returned result from the function. Since you are not returning anything from the function hence prints undefined.

To print 'Hi' in the second console you have to return that from the function.

function greet() {
  console.log("Hi");
  return 'Hi';
};

console.log(greet());

You should use a return keyword like this:

function greet() {
   console.log("HI");
   return "HI";
};
console.log(greet());

Or you can store it in a variable and return the variable:

function greet() {
   const hello = ("HI");
   return hello;
};
cosnole.log(greet());

,Because if you don't use return keyword and log the function to the console then it returns undefined.

The default behaviour of javascript functions is to return something if we don't manually return anything it will automatically return undefined

As you have defined your function greet it is not returning anything so by default the function greet is returning undefined

Your function call should be made in this way

function greet(){
    return "Hi";
}
console.log(greet());
发布评论

评论列表(0)

  1. 暂无评论