Undefined in console

hey,

i write this code and i get in the browser’s console the output “loli” but also undefined.

what is undefined here?
var ggh = “taylor”;
function happy(d){
var ggh = “loli” + d;
console.log(ggh);

          }
          console.log(happy("d"));

The function happy() doesn’t have a return value.

thanks @ArielLeslie! i added return to the function and undefined was clear.

why the return helps with the undefined?

If the function doesn’t return anything, what would you expect console.log() to print? The console.log() call logs whatever is inside the parentheses. If that is a function call, then it will log the returned value of that function. Since your function call doesn’t return a value, then that is undefined.

If your function doesn’t need to return anything, then there is no need to log its result to the console. If you just want the function to run, there is no need to call it inside a console.log(). You can always call a function by itself by just using

functionName(parameters);
2 Likes

I ve got the same thing with an other way:
function age(yearOfBirth ){
return 2019-yearOfBirth;
}
var ageOfJOHN = age(1918);
console.log(ageOfJOHN);
but without log it gives the result.
but it gave undefined with chrome and firefox;