Need help understanding the return function


#1

When I write this code it types out in the console to be "geek" instead of "Hey is Luke a nerd or a geek" but when I change the (return) function into console.log then it types it out. Why can't the (return) function do that too????

var hello = function(number,sailboat,windmill)
{
return "Hey is" , " " , number, " " , "a" , " " , sailboat, " ", "or is he a", " ", windmill;
};
hello("Luke", "Nerd" , "geek");

it types out in the console to be "geek" instead of "Hey is Luke a nerd or a geek" but when


#2

return is not a function, but an object we refer to as the return value.

A lot of people have a hard time wrapping their head around this and compare it to console.log(), for which there is no comparison.

There are two things that relate a function to its caller, arguments and return value. They travel down the same private channel to and from the function, always a direct and private connection between caller and function.

This means the caller can be in any context that still has the function in its scope chain. So we write a call to function x, and pass an argument. That value or object reference gets handed to a local variable defined in the function parameters and is now in that scope. It has travelled the private channel to this point. Now it gets worked upon and some resulting value or object gets sent back as the return object, down that private channel to the caller.


#3

Your function can return a string but your concatenation is wrong.

Here the corrected code
return "Hey is " + number + " a " + sailboat + " or is he a " + windmill;

This would return the correct string.


#4

@dragonflare,
console.log() is a so-called Method ( a log() function defined in the console =object= )
return is a so-called statement

=============================================================

If you use a comma-,
in the context of a return-statement
the rules defined for the comma-, -operator
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
are followed and
only the last element will be returned but all others are going to be evaluated as well
thus if you use

var hello = function(number,sailboat,windmill)
{
console.log("From within function");
return "Hey is" , " " , number, " " , windmill;
};
hello("Luke", "Nerd" , "geek");

the last element will be returned

=============================================================

If you use a comma-,
in the context of a console.log()
the comma-, will be used to concatenate a string AND for every comma-, a space-character-is-inserted
https://developer.mozilla.org/en-US/docs/Web/API/Console/log
https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/String

var hello = function(number,sailboat,windmill)
{
console.log("From within function");
console.log("Hey is" , " " , number, " " , "a" , " " , sailboat, " ", "or is he a", " ", windmill);
console.log("Returning value from within function");
return "Hey is" + " " + number+ " " + "a" + " " + sailboat+ " "+"or is he a"+ " "+ windmill;
};
console.log("\nCall of function without console.log");
hello("Luke", "Nerd" , "geek");
console.log("\nCall of function with console.log");
console.log(hello("Luke", "Nerd" , "geek"));