Console.log vs Return


#1

What is the difference between

var timesTwo = function(number) {
number * 2;
console.log(number*2)
};
timesTwo(8)

and

var timesTwo = function(number) {
return number * 2;};
timesTwo(8)

They both give output "16" to the console. Is console.log and return just an output method?


Computer Choice Part 2
#2

returns sends a value from a function back to the main scope returning to the main scope in the progress. And console.log just prints stuff in the console. If the function call is the last statement in the code you might as well see it on the screen as the console repeats the last unused value in the code (unless it is not existing -> undefined) but that does not make return an output to the screen.


#3

Is there a lesson further down the line that explains what you mean by main scope?


#4

There is one on global vs local. Main scope just means your program. Now if you add a function to your program this function has it's own scope meaning you're able to declare variables that only exist inside this function making the interior of the function a scope of its own. Therefore to distinguish the two I used the terms main scope and function scope.
Does this make sense?


#5

@haxor789
Can we use console log instead of return in a function to log the answer of the function to the console?


#6

Sure if you only want to print something use console.log. But this is not a replacement of return, return is meant to return to the main scope and if you don't use return on purpose the interpreter will add one for you automatically. So console.log and return are really two different topics.


#7

Thanks for the explanation! This is the first one that makes sense to me!