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
-
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 returnsundefined
by default when executed. Checkconsole.log((function(){})())
in your browser's console. – connexo Commented Jan 21, 2018 at 0:45 -
Hi
is not the overall result ofgreet()
, the function simply logs a message to the console. Looks like you actually wantreturn "Hi";
Functions that do not have areturn
value specified returnundefined
. – skyline3000 Commented Jan 21, 2018 at 0:50
3 Answers
Reset to default 8In 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());