Difference between console.log and just call function


#1

What is the difference with using console.log and just call the function. The proper answer displays anyway. At least in codecademy lesson.

var nameString = function (name) {
	return "Hi, I am" + " " +name
	
};
nameString("Artur");

display: Hi I am Artur
and

var nameString = function (name) {
	return "Hi, I am" + " " +name
	
};
console.log(nameString("Artur");)

display: Hi I am Artur


#2

@devcoder96779,

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

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

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#3

If you look at it closely you should see that the output differs at least for strings. For the first version you should see
'Hi, I am Artur' // in quotes
Where as the second one should produce
Hi, I am Artur //without quotes

This comes because console.log is used to print to the console whereas just calling the function replaces the function call with the returned value, which you can, as @leonhard.wettengmx.n mentioned, use to work with that value:

var theResult = myFunction( 4 );
console.log( theResult );

or use wherever you could use a value.

The reason why you see it on the screen has little to do with the function call itself but with the fact that it is the last value occurring in your code that is not used by a statement, therefore you see an echo of this value in the console. This will become clearer if you call the function twice with different inputs for console.log you'll see both function calls but for calling the function twice you'll only see the last.


#4

thank you very much guys